ios - 如何使用 AFNetworking 解析 JSON 处理 bool 值

标签 ios objective-c json

我有一些像这样返回的 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/

相关文章:

ios - 包含 UIStackViews 的 UITableViewCell 使用大量内存

java - 如何为可选参数创建哈希码和等于

java - jackson 设置默认 View

ios - LocationManager 坐标为零

ios - 如何以编程方式在 iOS 中播放 .flv 扩展文件

ios - 基于字典键的数组排序 :value in swift

c++ - OS X 双击速度

iPhone SDK : How to add an outline to UILabel's text

objective-c - 在 block 崩溃中分配 NSError 的调用方法

javascript - 如何将正则表达式从数组传递到字符串,当使用 .join ('' 时)例如 ["X","-","M","E","N"]