另一個JSON元素中得到JSON元素我有這樣一個JSON文件這樣:如何在Objective-C
{
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
,但我怎麼能深入去一個元素某一天之內?
非常感謝!
您想要獲取JSON結構中的哪個值? – Malloc
「我想在第2天提取Fajr的價值」。 2:38。謝謝! – samiles
這是不對的:'for(NSMutableDictionary * itemDict in result [@「dates」]){'。 itemDict將是關鍵,它是一個NSString,而不是字典。您需要轉身並使用itemDict選擇與itemDict項對應的值,然後才能查看內部字典。 –