1
{
   "Flight1":{
      "3":{
         "id":"10",
         "name":"JumboJet1B",
         "level":"1",
         "category":"1",
         "energy":"10",
         "bonus":"10",
         "completed":0
      },
      "4":{
         "id":"10",
         "name":"JumboJet1B",
         "level":"1",
         "category":"1",
         "energy":"10",
         "bonus":"10",
         "completed":0
      }
   }
}

This was the json output

How can I parse inside the items of 3 and 4, say getting the id, energy and name

Thanks!

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
Delta Boy
  • 11
  • 2

2 Answers2

1

If the order inside Flight1 doesn’t matter, the following should work:

NSDictionary *flights = … // result from a JSON parser
NSDictionary *flight1 = [flights objectForKey:@"Flight1"];

for (NSString *key in [flight1 allKeys]) {
    NSDictionary *flight1Entry = [flight1 objectForKey:key];

    NSString *entryId = [flight1Entry objectForKey:@"id"];
    NSString *entryName = [flight1Entry objectForKey:@"name"];
    NSString *entryEnergy = [flight1Entry objectForKey:@"energy"];

    …
}

Otherwise, if you want the keys sorted according to their numeric value:

NSDictionary *flights = … // result from a JSON parser
NSDictionary *flight1 = [flights objectForKey:@"Flight1"];
NSArray *flight1Keys = [[flight1 allKeys] sortedArrayUsingComparator:^(id o1, id o2) {
    NSInteger i1 = [o1 integerValue];
    NSInteger i2 = [o2 integerValue];
    NSComparisonResult result;

    if (i1 > i2) result = NSOrderedDescending;
    else if (i1 < i2) result = NSOrderedAscending;
    else result = NSOrderedSame;

    return result;
}];

for (NSString *key in flight1Keys) {
    NSDictionary *flight1Entry = [flight1 objectForKey:key];

    NSString *entryId = [flight1Entry objectForKey:@"id"];
    NSString *entryName = [flight1Entry objectForKey:@"name"];
    NSString *entryEnergy = [flight1Entry objectForKey:@"energy"];

    …
}
0

Assuming that you are using the json framework you could access it like this:

NSDictionary *jsonDict = [jsonString JSONValue];

NSString *id = [[[jsonDict objectForKey:@"Flight1"] objectForKey:@"3"] objectForKey:@"id"];

This assumes alot, so make use of try except blocks or iterate through the different levels.

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
  • Mr. Weaver I appreciate your answer but the thing is what if the keys 3,4 are dynamic they can change to 1,6,9,12,22 and so on. How can you parse it knowing only the key "Flight1"? –  Apr 19 '11 at 16:35
  • Use `valueForKey:` instead of `objectForKey:` and you won't have to worry about try except blocks. – tidwall Apr 20 '11 at 04:09
  • 2
    @jojaba What?! `-objectForKey:` is **the** method defined in `NSDictionary` to return an object for a given key. It won’t throw an exception if a key doesn’t exist — it simply returns `nil` in this case. –  Apr 20 '11 at 05:50
  • @Delta Boy your question asked for a way to retrieve the desired information. I actually hinted that you may want to iterate through the different levels. It's still your json structure, so your parser has to adapt to it. So if you'd like to parse the first two level use fast enumeration, which is available in dictionaries and arrays. – Nick Weaver Apr 20 '11 at 07:27
  • @Batarious The JSON library specified by Nick returns `NSString`, `NSNumber`, `NSArray` and `NSDictionary`. `-valueForKey:` works on ANY class while `-objectForKey:` does not. – tidwall Apr 20 '11 at 18:36