-2

I have a JSON document like so:

{
    dates =     {
        01 =         {
            date = "01-07-2013";
            prayers =             {
                Asr = "5:33";
                Dhuhr = "1:10";
                Fajr = "2:38";
                Isha = "11:34";
                Maghrib = "9:29";
                Qiyam = "1:37";
                Sunrise = "4:50";
            };
        };
        02 =         {
            date = "02-07-2013";
            prayers =             {
                Asr = "5:33";
                Dhuhr = "1:10";
                Fajr = "2:38";
                Isha = "11:34";
                Maghrib = "9:29";
                Qiyam = "1:37";
                Sunrise = "4:51";
            };
        };

    };
    location = "London";
}

I need to pull out the value for Fajr in day 02 (sometimes a different day, the JSON contains all days of month).

My code so far is this:

if(jsonData != nil)
{
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

    NSLog(@"%@", result);
    NSLog(@"finished");
    if (error == nil) {
        NSLog(@"finished");

        for (NSMutableDictionary *itemDict in result[@"dates"]) {

            NSLog(@"finished");
            NSLog(@"item dict: %@", itemDict);

            if ([itemDict isEqual: @"02"]) {
                //is dates
                NSLog(@"is 02");

                ///the next bit of code is wrong, needs fixing
                for (NSMutableDictionary *datesDict in itemDict[@"prayers"]) {

                    if ([datesDict isEqual: @"Fajr"]) {
                        NSString *morningTimeOfFajr = datesDict;
                        NSLog(@"time of Fajr: %@", morningTimeOfFajr);

                    }
                }
            }
        }
    }
}

Now this kind of works to pull out the data for dates, but how can I drill down to get to one element within a given day?

Many thanks!

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
samiles
  • 3,768
  • 12
  • 44
  • 71
  • Which value in the JSON structure you want to get ? – Malloc Jul 06 '13 at 01:58
  • "I want to pull out the value for Fajr in day 2". So, 2:38. Thanks! – samiles Jul 06 '13 at 02:03
  • This isn't right: `for (NSMutableDictionary *itemDict in result[@"dates"]) {`. itemDict will be the key, which is an NSString, not a dictionary. You need to turn around and use itemDict to select the value corresponding to the item itemDict key before you can look inside the inner dictionary. – Hot Licks Jul 06 '13 at 02:18

2 Answers2

3

The quick way:

NSString *f = [result valueForKeyPath:@"dates.02.prayers.Fajr"];

Actually, I'm not certain that'll work -- not sure the numerical key "02" will work in a key path. But this should be fine:

NSString *f = result[@"dates"][@"02"][@"prayers"][@"Fajr"];

There are more verbose ways to say the same thing, and it wouldn't hurt to do some error checking, but the idea is the same -- you've got a bunch of nested dictionaries, and all you have to do is to access the right node at each level.

Caleb
  • 124,013
  • 19
  • 183
  • 272
2

"I want to pull out the value for Fajr in day 2"

Sometimes it's best to do it a step at a time. It's a lot easier to debug, and the logic is easier to understand (especially if you use meaningful names for the intermediate results):

NSString* dayToFetch = @"02";
NSString* prayerToFetch = @"Fajr";

id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
<Check for errors>
NSDictionary* dates = [result objectForKey:@"dates"];
NSDictionary* day = [dates objectForKey:dayToFetch];
if (day == nil) {
    <Handle error>
}
NSDictionary* prayers = [day objectForKey:@"prayers"];
NSString* prayerTime = [prayers objectForKey:prayerToFetch];
if (prayerTime == nil) {
    <Handle error>
}

Though you certainly can substitute the new [] notation for objectForKey if you wish. I'm just used to using the older style. Something like:

NSString* dayToFetch = @"02";
NSString* prayerToFetch = @"Fajr";

id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
<Check for errors>
NSDictionary* dates = result[@"dates"];
NSDictionary* day = dates[dayToFetch];
if (day == nil) {
    <Handle error>
}
NSDictionary* prayers = day[@"prayers"];
NSString* prayerTime = prayers[prayerToFetch];
if (prayerTime == nil) {
    <Handle error>
}
Hot Licks
  • 47,103
  • 17
  • 93
  • 151