2014-11-24 219 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狩猎!