-2

我有一个像这样的 JSON 文档:

{
    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";
}

我需要提取Fajr当天的价值02的值(有时是不同的一天,JSON 包含一个月中的所有天数)。

到目前为止,我的代码是这样的:

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);

                    }
                }
            }
        }
    }
}

现在这种工作可以提取数据dates,但是我如何在给定的一天内深入了解一个元素?

非常感谢!

4

2 回答 2

3

快捷方式:

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

实际上,我不确定这会起作用——不确定数字键“02”是否会在键路径中起作用。但这应该没问题:

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

有更冗长的方式来表达同样的事情,做一些错误检查也没有什么坏处,但想法是一样的——你有一堆嵌套的字典,你所要做的就是访问每个级别的正确节点。

于 2013-07-06T02:06:15.040 回答
2

“我想在第 2 天提取 Fajr 的值”

有时最好一次做一步。调试起来要容易得多,逻辑也更容易理解(尤其是如果您对中间结果使用有意义的名称):

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>
}

尽管您当然可以根据需要替换新[]符号objectForKey。我只是习惯使用旧样式。就像是:

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>
}
于 2013-07-06T02:28:22.723 回答