我尝试在Xcode 6中解析JSON,我的代码如下所示:
NSData *jsonClusterHealth = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://XXX.XXX.XXX.XXX:XXXX/_cluster/health?pretty=true"]];
id jsonCHealth = [NSJSONSerialization JSONObjectWithData:
jsonClusterHealth options:NSJSONReadingMutableContainers error:nil];
ClusterStatusLabel.text = [jsonCHealth objectForKey:@"status"];
//NumberOfNodesLabel.text = [jsonCHealth objectForKey:@"number_of_nodes"];
//ActiveShardsLabel.text = [jsonCHealth objectForKey:@"active_shards"];
所以,一切都很好,直到我取消注释最后两行,它崩溃应用程序时出现以下错误:
-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000013 2014-12-12 11:00:35.447 FnHAdmin[22139:1077781] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000013'
我发现原因是json的对象键中的下划线...那么Xcode中的问题是什么,json键包含下划线???主要问题是如何解决它(我不能删除JSON中的下划线,因此解决方案必须在Xcode中)...
答案 0 :(得分:1)
从崩溃日志看来,密钥@"number_of_nodes"
和@"active_shards"
的对象似乎是NSNumber类型。尝试,
NumberOfNodesLabel.text = [NSString stringWithFormat:@"%@", [jsonCHealth objectForKey:@"number_of_nodes"]];
ActiveShardsLabel.text = [NSString stringWithFormat:@"%@", [jsonCHealth objectForKey:@"active_shards"]];