2013-07-06 18 views
-3

另一個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"; 
} 

我需要拉出值Fajr02天(有時是不同的一天, 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,但我怎麼能深入去一個元素某一天之內?

非常感謝!

+0

您想要獲取JSON結構中的哪個值? – Malloc

+0

「我想在第2天提取Fajr的價值」。 2:38。謝謝! – samiles

+0

這是不對的:'for(NSMutableDictionary * itemDict in result [@「dates」]){'。 itemDict將是關鍵,它是一個NSString,而不是字典。您需要轉身並使用itemDict選擇與itemDict項對應的值,然後才能查看內部字典。 –

回答

3

的快捷方式:

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

其實,我不能肯定會工作 - 不知道的數字鍵「02」將在關鍵路徑工作。但是,這應該是罰款:

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

有更詳細的方式說同樣的事情,而且不會傷害做一些錯誤檢查,但這個想法是一樣的 - 你有一堆嵌套字典,你所要做的就是訪問每個級別的正確節點。

+0

第二個很棒,很簡單。謝謝! – samiles

2

「我想拉出值晨2日」

有時,最好做一次一個步驟。它更容易調試了很多,邏輯很容易理解(特別是如果你使用的中間結果有意義的名稱):

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