2

I am struggling with the following problem:

I've got an API which returns a nested JSON piece. (I use NSJSONSeralization to parse it)

eg:

 { "thing" = 1,
   "Other thing" = 2,
   "ParentFromList": [{
   "IT" = 3,
   "SecondIT" = 4
 }

How can I use IT and Second it?

I've tried:

  NSDictionary *thingy = [[jsonOutput objectForKey:@"ParentFromList"] ObjectForKey:@"IT"];

JsonOutput is also a nsdictionary. But after running it, it fails with:

  Unrecognized Selector send to instance.

I don't know how to fix this, help appreciated.

1
  • Just remember that {} means a dictionary and [] means its a array. Commented Dec 13, 2012 at 15:47

1 Answer 1

1

For your data, the outmost object is a NSDictionary instance. The value for the key ParentFromList is an array, i.e. a NSArrayinstance (note the brackets). The arrays first element contains another dictionary instance (note the curly braces).

Furthermore, it's not ObjectForKey: but objectForKey: (case matters).

So you probably want to write:

NSDictionary *thingy = [ [ [jsonOutput objectForKey: @"ParentFromList"]
                         objectAtIndex: 0]
                       objectForKey: @"IT"];

BTW: Your sample JSON data is both incomplete and not in JSON format (it's Apple's property list format).

The correct and complete JSON representation would be:

{
    "thing": 1,
    "Other thing": 2,
    "ParentFromList": [
        {
            "IT": 3,
            "SecondIT": 4
        }
    ]
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Also the JSON part was just for explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.