{"id":13489221,"url":"https://github.com/mrap/SwiftJSONParser","last_synced_at":"2025-03-28T04:30:57.634Z","repository":{"id":20147537,"uuid":"23417904","full_name":"mrap/SwiftJSONParser","owner":"mrap","description":"Parse JSON like a badass","archived":false,"fork":false,"pushed_at":"2015-12-13T00:26:19.000Z","size":39,"stargazers_count":50,"open_issues_count":0,"forks_count":7,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-31T02:33:35.617Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://github.com/mrap/SwiftJSONParser","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-28T06:34:21.000Z","updated_at":"2024-05-13T10:14:16.000Z","dependencies_parsed_at":"2022-08-21T17:40:11.411Z","dependency_job_id":null,"html_url":"https://github.com/mrap/SwiftJSONParser","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrap%2FSwiftJSONParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrap%2FSwiftJSONParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrap%2FSwiftJSONParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrap%2FSwiftJSONParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrap","download_url":"https://codeload.github.com/mrap/SwiftJSONParser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245970371,"owners_count":20702396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T19:00:20.331Z","updated_at":"2025-03-28T04:30:57.378Z","avatar_url":"https://github.com/mrap.png","language":"Swift","funding_links":[],"categories":["Libs"],"sub_categories":["Data Management"],"readme":"# Parse JSON like a badass\n\n### Full disclaimer regarding performance\n\nFor production apps (or if you care about efficiency at all),\nI highly recommend using [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON)\nover this library. After benchmarking, I found that SwiftJSONParser could be\nup to 7x slower than SwiftyJSON.\n\n### This sucks\n\n```swift\nlet jsonData = NSData(contentsOfURL: NSURL(string: url))\nvar jsonErrorOptional: NSError?\nlet jsonOptional: AnyObject! = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions(0), error: \u0026jsonErrorOptional)\n\nif let json = jsonOptional as? Dictionary\u003cString, AnyObject\u003e {\n    if let other = json[\"other\"] as? Dictionary\u003cString, AnyObject\u003e {\n        if let nicknames = other[\"nicknames\"] as? Array\u003cString\u003e {\n            if let handle = nicknames[0] as AnyObject? as? String {\n                println(\"Some folks call me \\(handle)\")\n            }\n        }\n    }\n}\n```\n\n### This is readable\n\n```swift\nlet jsonData = NSData(contentsOfURL: NSURL(string: url))\nlet parser = JSONParser(jsonData)\n\nif let handle = parser.getString(\"other.nicknames[0]\") {\n    println(\"Some folks call me \\(handle)\")\n}\n```\n\n\n## Usage\n\nSample JSON payload we want to parse\n\n    {\n        \"name\": \"Mike\",\n        \"favorite_number\": 19,\n        \"gpa\": 2.6,\n        \"favorite_things\": [\"Github\", 42, 98.6],\n        \"other\": {\n            \"city\": \"San Francisco\",\n            \"commit_count\": 9000,\n            \"nicknames\": [\"mrap\", \"Mikee\"]\n        }\n    }\n\nGet values of a specific type. Returns optionals\n\n```swift\nif let name = parser.getString(\"name\") {\n    println(\"My name is \\(name)\")\n}\n\nif let number = parser.getInt(\"favorite_number\") {\n    println(\"\\(number) is my favorite number!\")\n}\n\nif let gpa = parser.getDouble(\"gpa\") {\n    println(\"My stellar highschool gpa was \\(gpa)\")\n}\n```\n\nOr get `AnyObject` if you're not sure\n\n```swift\nif let city = parser.get(\"other.city\") {\n    // city will be type AnyObject\n}\n```\n\nGet an Array of values\n\n```swift\nif let favorites = parser.getArray(\"favorite_things\") {\n    // favorites =\u003e [\"Github\", 42, 98.6]\n}\n```\n\n## Error Handling\n\nIf you're not sure about incoming json data, check it first\n\n```swift\nlet badJsonData = NSData(contentsOfURL: NSURL(string: url))\nlet parser = JSONParser(badJsonData)\n\n// Check for an error\nif parser.error != nil {\n   // Do stuff with json\n} else {\n   // Handle the error\n   println(parser.error)\n}\n```\n\n## Installation\nThe best way to use SwiftJSONParser is to import it as a framework.\n\n1.  Clone a local copy of SwiftJSONParser in any directory you'd like.\n``\ngit clone git@github.com:mrap/SwiftJSONParser.git\n``\n2. Drag `SwiftJSONParser.xcodeproj` into your project's project navigator\n\n3. Add SwiftJSONParser as a Target Dependency.\n- Go to your App Target \u003e Build Phases\n- In Target Dependencies, press `+`, select `SwiftJSONParser`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrap%2FSwiftJSONParser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrap%2FSwiftJSONParser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrap%2FSwiftJSONParser/lists"}