JSONparsing发送到实例0x8f3ab10的无法识别的select器
我试图parsing一些非常基本的JSON数据,我做的是完全一样的乳清,我总是这样做,但我得到以下错误:
– [__ NSCFDictionary objectAtIndex:]:无法识别的select器发送到实例0x8e7d570 2014-04-30 15:04:33.699 Mensagens 2 [7530:60b] *终止应用程序由于未捕获exception'NSInvalidArgumentException',原因:' – [__ NSCFDictionary objectAtIndex:] :无法识别的select器发送到实例0x8e7d570'
这是GET.JSON文件:
{ "version":"6" }
这是我的应用程序中的代码,我尝试获取版本的价值:
NSURL *url = [NSURL URLWithString:@"http://localhost/app/get.json"]; NSData *data = [NSData dataWithContentsOfURL:url]; NSMutableArray *getVersion = [[NSMutableArray alloc]init]; getVersion = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; NSString *currentVersion = [[getVersion objectAtIndex:0]objectForKey:@"version"]; NSLog(@"Version = %@", currentVersion);
我只是没有看到哪里出了问题。
它应该是
NSDictionary *getVersion = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; NSString *currentVersion = [getVersion objectForKey:@"version"];
或者用新的语法
NSString *currentVersion = getVersion[@"version"];
因为objectAtIndex
意味着你正在访问一个数组,但你只有一个JSON对象
不pipe您用来声明getVersion的types,下面的方法根据传递给此方法的数据创buildNSDictionary或NSArray。 在你的情况下,似乎JSON响应数据是一个字典,因此,即使getVersion声明(或甚至实例化)为NSMutableArray对象,执行下面的getVersion是一个NSDictionary。
getVersion = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
正如meda所回答的,你可以使用[getVersion objectForKey:@“Version”]来获得你要找的东西。