2014-11-24 220 views
1

接收到以下錯誤。我相信它與JSON數組結果中的NSDictionary有關。即NSDictionary內的NSDictionary也許?JSON ObjectiveC - 錯誤

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7f9298554560' 

這是調用時的JSON服務。這是從XCode直接打印的,但對我來說看起來很乾淨。

Printing description of self->metArray: 
{ 
    weatherObservation =  { 
     ICAO = YBBN; 
     clouds = "few clouds"; 
     cloudsCode = FEW; 
     countryCode = AU; 
     datetime = "2014-11-24 03:00:00"; 
     dewPoint = 20; 
     elevation = 5; 
     hectoPascAltimeter = 1014; 
     humidity = 61; 
     lat = "-27.38333333333333"; 
     lng = "153.1333333333333"; 
     observation = "YBBN 240300Z 02019KT 9999 FEW029 SCT250 28/20 Q1014"; 
     stationName = "Brisbane Airport M. O"; 
     temperature = 28; 
     weatherCondition = "n/a"; 
     windDirection = 20; 
     windSpeed = 19; 
    }; 
} 

這是調用JSON服務的代碼。

-(void)getMetar{ 

    // NSString *location = @"YBBN"; 
    NSString * const metarUrl [email protected]"http://api.geonames.org/weatherIcaoJSON?ICAO=YBBN&username=demo"; 

    NSURL *url2 = [NSURL URLWithString:metarUrl]; 
    NSData *data2 = [NSData dataWithContentsOfURL:url2]; 

    metArray = [NSJSONSerialization JSONObjectWithData:data2 options:kNilOptions error:nil]; 

    //Create an NSDictionary for the weather data to be stored. 
    NSDictionary *metarJson = [NSJSONSerialization JSONObjectWithData:data2 options:kNilOptions error:nil]; 

    //Loop through the JSON array 
    NSArray *currentMetarArray = metarJson[@"weatherObservation"]; 

    //set up array and json call 
    metarArray = [[NSMutableArray alloc]init]; 
    for (NSDictionary *metaritem in currentMetarArray) 
    { 
     //create our object 
     NSString *nClouds = [metaritem objectForKey:@"clouds"]; 
     NSString *nObservation = [metaritem objectForKey:@"observation"]; 

     //Add the object to our animal array 
     [metarArray addObject:[[metar alloc]initWithclouds:(nClouds) andobservation:nObservation]]; 

    } 
} 

它看起來不錯,但也許這是因爲我一直在看它幾個小時。

任何想法?

+0

谷歌「無法識別的選擇器」,看看它是什麼意思。然後學習如何獲得適當的異常堆棧跟蹤,以便識別失敗的線路。 – 2014-11-24 03:57:44

+0

但有一點是你的JSON中沒有數組。轉到json.org並學習JSON語法。 – 2014-11-24 03:58:58

+0

嗨熱舔,好點我沒有想過如何得到「無法識別的選擇器」的搜索。 iOS仍然是新的,但每一天似乎都越來越好,謝謝! – Jeremy 2014-11-24 04:36:07

回答

1

我去你的網址,並得到這個消息{"status":{"message":"the daily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.","value":18}}

不過你可以試試我的代碼,它爲我工作

NSString *strUrl = @"http://api.geonames.org/weatherIcaoJSON?ICAO=YBBN&username=demo"; 
NSURL *url = [NSURL URLWithString:strUrl]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 

NSError *error; 

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 

NSDictionary *weatherObservation = [json objectForKey:@"status"]; 

for (id key in weatherObservation) { 
    id value = [weatherObservation objectForKey:key]; 

    NSLog(@"key = %@, value = %@", key, value); 
} 
+0

嗨Minh,抱歉忘了提及我拿出我的用戶憑據爲了安全。我確實收到信息。我會現在審查你的建議。謝謝! – Jeremy 2014-11-24 04:35:00

+0

@Jeremy,不客氣! – 2014-11-24 04:37:01

+0

好吧,我明白你已經做了什麼。你所做的是正確的。我只是想找出返回值中的每個元素。即for循環內的溫度,風速等。 – Jeremy 2014-11-24 04:53:55

2
NSArray *currentMetarArray = metarJson[@"weatherObservation"]; 
for (NSDictionary *metaritem in currentMetarArray) 

這些線是錯誤的。 currentMetarArray是一個字典,而不是陣列,它包含字符串鍵&值,因此應該訪問它像在以下way-

的NSDictionary * jsonDictionary = [NSJSONSerialization JSONObjectWithData:數據選項:0錯誤:&錯誤]; NSDictionary * weatherObservation = [jsonDictionary objectForKey:@「weatherObservation」];

然後獲取nCloud,使用類似於 NSString * nClouds = [weatherObservation objectForKey:@「clouds」];

+0

嗨,羅素,釘上它!男人,你和Minh幫助如果我可以給我學分!我認爲有兩個字典是關鍵只是不知道語法如何做(仍然是新手狀態)。真棒醬人。 – Jeremy 2014-11-24 04:59:02

+0

@傑瑞米,很高興認識這個幫助你的人:) – russell 2014-11-24 05:06:11

+0

完全有幫助。你和Minh都是。再次感謝你們! – Jeremy 2014-11-24 05:12:35

0

拋出的錯誤是如何解決這個問題的最佳線索。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7f9298554560' 

這是什麼意思是,你認爲一個對象是一個字典,但實際上它不是。

可以解決這個使用NSObject的方法,如:

[myObject respondsToSelector:(@selector(objectForKey:))]; // will return a boolean value. 

,看物體會向你希望把它

[myObject isKindOfClass:[NSDictionary class]]; // will return a boolean value. 

確定方法作出迴應如果該對象是一個NSDictionary。

但是,可能發生的情況是,您對反序列化JSON數據的期望有誤。

快樂的bug狩獵!