接收到以下错误。我相信它与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