解析JSON时无法识别的选择器

时间:2015-02-11 04:21:39

标签: ios objective-c json unrecognized-selector

所以我下面的照片是我拍摄的JSON脚本的截图,我试图从URL中读取。在下面的代码部分中,我设法得到了'名称'属性并将其分配给' name' NSString对象,但是当我尝试为'地址'做同样的事情时我收到以下错误:

[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x174260980
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x174260980'

我在内部'场地'之间的区别是什么?比在里面'位置'?我尝试了很多不同的方法,有些包括数组,但似乎没有任何工作。

enter image description here

NSURL *url = [NSURL URLWithString:urlString];

NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

NSDictionary *responseDictionary = [dataDictionary objectForKey:@"response"];
NSDictionary *venueDictionary = [responseDictionary objectForKey:@"venues"];

for (NSDictionary *dict in venueDictionary) {
    NSLog(@"%@", dict);
    NSString *name = [dict objectForKey:@"name"];
    NSDictionary *locationDictionary = [dict objectForKey:@"location"];
    for (NSDictionary *locationDict in locationDictionary) {
        NSString *address = [locationDict objectForKey:@"address"];
        NSLog(@"%@", address);
    }
}

1 个答案:

答案 0 :(得分:3)

你期望“location”键指向一个数组,但事实并非如此。它指向一个对象,摆脱for (NSDictionary *locationDict in locationDictionary) {而只是从locationDictionary中取出地址。