gpt4 book ai didi

ios - Xcode 解析带下划线的 JSON

转载 作者:行者123 更新时间:2023-11-29 02:26:41 26 4
gpt4 key购买 nike

我尝试在 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 中)...

最佳答案

从崩溃日志来看,键 @"number_of_nodes"@"active_shards" 的对象似乎是 NSNumber 类型。试试看,

NumberOfNodesLabel.text = [NSString stringWithFormat:@"%@", [jsonCHealth objectForKey:@"number_of_nodes"]];
ActiveShardsLabel.text = [NSString stringWithFormat:@"%@", [jsonCHealth objectForKey:@"active_shards"]];

关于ios - Xcode 解析带下划线的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27441021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com