我有一些像这样返回的 JSON:
"items":[
{
"has_instore_image": false
}
]
如果我输出这样的值:
NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]);
我明白了
has_instore_image val: 0
但是如果我这样测试:
if([item objectForKey:@"has_instore_image"]==0){
NSLog(@"no, there is not an instore image");
}else{
...
它总是转到 else 语句...嗯...您建议我如何获取 BOOL 值并进行测试?我已经通读了这里的 BOOL 问题,只是感到困惑,这并没有像我预期的那样工作。
谢谢
最佳答案
NSDictionary
的实例方法 objectForKey
返回一个 id
,而不是原始值。
如果它是 boolean
, int
, float
等 JSON 中类似数字的值,它将序列化为 NSNumber
通过 Apple 的 NSJSONSerialization
类和 iOS 中的大多数/所有其他常见 JSON 解析器。
如果你想从中得到 BOOL
值,你可以这样做:
BOOL has_instore_image = [[item objectForKey:@"has_instore_image"] boolValue];
关于ios - 如何使用 AFNetworking 解析 JSON 处理 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21902262/