2015-02-11 67 views
0

因此,我有下面的照片,這是我嘗試從URL中讀取的JSON腳本的屏幕截圖。在下面的代碼段,我設法拿到了「name」屬性,並將其分配到「姓名」的NSString對象,但是當我嘗試「地址」我得到以下錯誤做同樣的:解析JSON時無法識別的選擇器

[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x174260980 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x174260980' 

「場地」內部比「內部」內部做得怎麼樣?我嘗試了許多不同的方式來做到這一點,其中一些包括數組,但似乎沒有任何工作。

enter image description here

NSURL *url = [NSURL URLWithString:urlString]; 

NSData *jsonData = [NSData dataWithContentsOfURL:url]; 
NSError *error = nil; 
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; 

NSDictionary *responseDictionary = [dataDictionary objectForKey:@"response"]; 
NSDictionary *venueDictionary = [responseDictionary objectForKey:@"venues"]; 

for (NSDictionary *dict in venueDictionary) { 
    NSLog(@"%@", dict); 
    NSString *name = [dict objectForKey:@"name"]; 
    NSDictionary *locationDictionary = [dict objectForKey:@"location"]; 
    for (NSDictionary *locationDict in locationDictionary) { 
     NSString *address = [locationDict objectForKey:@"address"]; 
     NSLog(@"%@", address); 
    } 
} 
+0

在分配之前,您只需執行NULL檢查 – 2015-02-11 04:24:49

+1

@AlbinJoseph它不爲null,因爲如果我嘗試在for循環中打印'locationDict',我將獲得所有位置類別(地址,緯度,長度,距離等)。 – 2015-02-11 04:30:35

+0

哪一行代碼導致錯誤?您發佈的任何代碼都沒有提供數組引用。 – rmaddy 2015-02-11 04:31:04

回答

3

你期待的「位置」鍵指向一個數組,但事實並非如此。它指向一個對象,擺脫for (NSDictionary *locationDict in locationDictionary) {而只是將地址從locationDictionary中取出。

相關問題