-2

I have an NSDictionary that is getting a JSON file. This is my JSON file:

{
"People": {
    "Downtown": [
        {
            "firstName": "Steve",
            "lastName": "Smith",
            "Color": "Blue",
        },
        {
            "firstName": "John",
            "lastName": "Doe",
            "Color": "Red",
        }

      ]
}
}

In Xcode I used a NSLog to display the whole file and it works, but now I would like to call on for example the second object in the JSON array and extract the last name giving me the value "Doe" allowing me to put it in a NSString in Xcode.

I tried

NSLog(@"This is in dictionary: %@", [dictionary objectForKey: @"People"]);

in Xcode, witch got me closer but still pretty far. I'm thinking it's something along the lines of –objectForKey:, but Im pretty new to NSDictionary so I don't know all the syntax or how to use it properly.

holex
  • 23,961
  • 7
  • 62
  • 76
Alessandro Kreslin
  • 269
  • 1
  • 3
  • 15
  • possible duplicate of [How do I parse JSON with Objective-C?](http://stackoverflow.com/questions/5547311/how-do-i-parse-json-with-objective-c) – carloabelli Jul 11 '14 at 13:26

2 Answers2

0
NSLog(@"Dict:%@",[[[[Dict valueForKey:@"People"] valueForKey:@"Downtown"] valueForKey:@"lastName"] objectAtIndex:1]);

Hope it helps.

CW0007007
  • 5,681
  • 4
  • 26
  • 31
Iphonenew
  • 299
  • 2
  • 11
  • it looks to me you put the array at wrong location. the `Downtown` has the array not the `lastName`, I guess. – holex Jul 11 '14 at 14:43
0

Try This:

NSLog(@"This is in dictionary: %@", [dictionary valueForKeyPath: @"People"]);
NSLog(@"This is in dictionary: %@", [dictionary valueForKeyPath:@"People.Downlotwn.Firstname"]); 

etc

If you are getting null, try to remove Key: People at start.

Anton
  • 3,102
  • 2
  • 28
  • 47