接收到以下錯誤。我相信它與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]];
}
}
它看起來不錯,但也許這是因爲我一直在看它幾個小時。
任何想法?
谷歌「無法識別的選擇器」,看看它是什麼意思。然後學習如何獲得適當的異常堆棧跟蹤,以便識別失敗的線路。 – 2014-11-24 03:57:44
但有一點是你的JSON中沒有數組。轉到json.org並學習JSON語法。 – 2014-11-24 03:58:58
嗨熱舔,好點我沒有想過如何得到「無法識別的選擇器」的搜索。 iOS仍然是新的,但每一天似乎都越來越好,謝謝! – Jeremy 2014-11-24 04:36:07