收到以下错误。我相信它与JSON数组的结果中的NSDictionary有关。即NSDictionary中的NSDictionary可能吗?
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 =@"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 :(得分:2)
NSArray *currentMetarArray = metarJson[@"weatherObservation"];
for (NSDictionary *metaritem in currentMetarArray)
这些行是错误的。 currentMetarArray是一个字典,不是数组,它包含字符串键&值,所以你应该像下面那样访问它 -
NSDictionary * jsonDictionary = [NSJSONSerialization JSONObjectWithData:数据选项:0错误:&错误]; NSDictionary * weatherObservation = [jsonDictionary objectForKey:@" weatherObservation"];
然后获得nCloud,使用类似 NSString * nClouds = [weatherObservation objectForKey:@" clouds"];
答案 1 :(得分: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);
}
答案 2 :(得分: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数据的期望有误。
快乐的虫子狩猎!