Ios 如何使用AFJSON处理布尔值
我有一些JSON返回如下:Ios 如何使用AFJSON处理布尔值,ios,objective-c,json,Ios,Objective C,Json,我有一些JSON返回如下: "items":[ { "has_instore_image": false } ] NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]); if([item objectForKey:@"has_instore_image"]==0){ NSLog(@"no, there is not an instore image"); }else{ ... BOOL
"items":[
{
"has_instore_image": false
}
]
NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]);
if([item objectForKey:@"has_instore_image"]==0){
NSLog(@"no, there is not an instore image");
}else{
...
BOOL has_instore_image = [[item objectForKey:@"has_instore_image"] boolValue];
如果我这样输出值:
"items":[
{
"has_instore_image": false
}
]
NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]);
if([item objectForKey:@"has_instore_image"]==0){
NSLog(@"no, there is not an instore image");
}else{
...
BOOL has_instore_image = [[item objectForKey:@"has_instore_image"] boolValue];
我明白了
has_instore_image val: 0
但如果我这样测试:
"items":[
{
"has_instore_image": false
}
]
NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]);
if([item objectForKey:@"has_instore_image"]==0){
NSLog(@"no, there is not an instore image");
}else{
...
BOOL has_instore_image = [[item objectForKey:@"has_instore_image"] boolValue];
它总是转到else语句。。。六羟甲基三聚氰胺六甲醚。。您建议我如何获得布尔值和测试?我已经通读了这里的BOOL问题,只是感到困惑,这不是我预期的工作
thx您正在将指针与整数进行比较
[item objectForKey:@"has_instore_image"]==0
你应该使用
[item objectForKey:@"has_instore_image"].integerValue==0
还要注意的是NO
的BOOL
等于0
代码中的
NSLog
语句打印一个0,但这仅仅是因为如果将NSLog
对象作为参数,则调用对象description
。NSDictionary
的实例方法objectForKey
返回一个id
,而不是原始值
如果它是JSON中的boolean
、int
、float
等类似数字的值,它将被苹果的NSJSONSerialization
类和iOS中大多数/所有其他常见的JSON解析器序列化为NSNumber
如果要从中获取BOOL
值,可以执行以下操作:
"items":[
{
"has_instore_image": false
}
]
NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]);
if([item objectForKey:@"has_instore_image"]==0){
NSLog(@"no, there is not an instore image");
}else{
...
BOOL has_instore_image = [[item objectForKey:@"has_instore_image"] boolValue];
我建议将这些id类型(从字典返回)保留为NSNumber
NSNumber *boolNum=(NSNumber*)[item objectForKey:@"has_instore_image"];
之后,您可以从boolNum获得bool值
[boolNum boolValue]
试试这个
if([boolNum boolValue]==NO){
NSLog(@"no, there is not an instore image");
}else
{
}