-2

I have retrieved the following raw data from an API call and need to access it's components. At this point, I am fairly inexperienced with NSDictionaries within Objective-C and am struggling to understand the correct data types required to parse and access the individual JSON elements.

My raw JSON is as follows

[ { "id": 1, "forename": "Christian", "surname": “Smith”, "email": "christian@host.com”, "company_id": 1, "company_position": "Managing Director", "twitter": “@ChristianSmith”, "linkedin": "http://linkedin.com/Christian", "telephone": "0123” 123 1231, "website": "http://www.test.com”, "profile_image": "15394045184900bcdcd027fef6b5f9f1.png", "enabled": 1, "admin": 0, "created_at": "2015-01-20 14:27:33", "updated_at": "2015-02-09 15:25:44" }, { "id": 2, "forename": "Ross", "surname": “Smith”, "email": "ross@host.com”, "company_id": 2, "company_position": "Web Artisan", "twitter": “@RossSmith”, "linkedin": "http://www.linkedin.com/ross", "telephone": “01213211513, "website": "http://www.test.com, "profile_image": "6b079f50cf977f52c8073cc0b11d9dc6.png", "enabled": 1, "admin": 1, "created_at": "2015-01-20 14:27:33", "updated_at": "2015-02-09 16:13:31" } ]

Assuming my raw JSON is held in:

NSData *allUsersJSONData = [allUsersJSON dataUsingEncoding:NSUTF8StringEncoding];

My next step is to parse the JSON data into an accessible form. This is where my confusion arises. Conceptually, should I use an NSDictionary or an NSArray when parsing the JSON data?

I understand I can use the following code to parse the data

[NSJSONSerialization JSONObjectWithData:allUsersJSONData options:kNilOptions error:&error];

However I am struggling to understand the terminology involved and exactly what is being returned by the above function. Any explanation of the above would be greatly appreciated.

Scratcha
  • 1,359
  • 1
  • 14
  • 22
  • 2
    Always consider Googling first. A query for `parsing JSON with Objective C` will turn up many hits with great, detailed examples. – Pekka Feb 12 '15 at 13:40
  • possible duplicate of [How do I parse JSON with Objective-C?](http://stackoverflow.com/questions/5547311/how-do-i-parse-json-with-objective-c) – Pekka Feb 12 '15 at 13:40
  • 1
    First go to json.org and learn the JSON syntax. It only takes 5-10 minutes to learn. – Hot Licks Feb 12 '15 at 13:50
  • 1
    And if you don't know what an NSArray or NSDictionary is, look them up. – Hot Licks Feb 12 '15 at 13:51
  • Thanks Hot Licks - I think it was my lack of attention to the JSON syntax. Being able to properly interpret it has made things much more coherant in terms of what data types to use. Thanks for the advice. – Scratcha Feb 12 '15 at 14:30

2 Answers2

0

Your first step is nonsense. I bet you received NSData from a server and turned it into an NSString (which was pointless). The next step that you are trying would turn that string into JSON data representing one single very, very long string. That's nonsense.

Take the NSData that you got from the server unchanged and put it into JSONObjectWithData.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

Go to GitHub and check out SDFeedParser. It's a pretty good introduction to parsing JSON in my opinion.

jpmcgrath
  • 1
  • 1