Json布尔值表达式问题

时间:2013-07-23 11:35:11

标签: iphone ios objective-c json ios6

我的回复:(JSON)

NSString *responseString = [[NSString alloc] initWithData:responseData4 encoding:NSUTF8StringEncoding];

NSLog(@"responseString... %@",responseString);

 {"AT":"dMaOtJxV0EgAAAAAAAAAAAAAAAAAAAAA","DId":320,"EMs":null,"Em":null,"LGT":null,"OS":"Success","PWD":"DEBC0C7321EF1386F7F22A3D8BE754C9FF04A596","SId":0,"Sku":"Director","UId":1664,"UN":"s1","COM":null,"DN":null,"DOS":null,"EA":"s+1@gmail.com","FN":"s","IsCon":true,"IsLT":true,"IsPU":false,"IsSuc":false,"IsSync":true,"IsTri":false,"LLD":"\/Date(-62135578800000-0500)\/","LN":"k","MOD":null,"NDD":"\/Date(1376971200000-0400)\/","NDDS":"8\/20\/2013","PN":"(324) 325-4353","SD":"s","SLT":"HBoY7mHJ","STs":"EN","UQId":null,"US":3,"Ver":null}

我必须在这里获取IsPU,IsCon标记值。

然后我编写了像

这样的代码
NSError *error;

id jsonMain = [NSJSONSerialization JSONObjectWithData:responseData4 options:kNilOptions error:&error];

NSString *IsPUstring = [jsonMain objectForKey:@"IsPU"];

NSString *IsConstring = [jsonMain objectForKey:@"IsCon"];


NSLog(@"IsPUstring...%@",IsPUstring);

NSLog(@"IsConstring...%@",IsConstring);

输出:

  

IsPUstring ... 0

     

IsConstring ... 1

我的问题:

原始标记值:

  

“ISCON”:真 “ISPU”:假

但是在JSON序列化后,它变为0和1对吗?

有没有办法让我的原始值如真或假?

2 个答案:

答案 0 :(得分:1)

你可以像吼叫一样......

 BOOL  IsPUstring  = [[jsonMain objectForKey:@"IsPU"] boolValue];
 BOOL IsConstring = [[jsonMain objectForKey:@"IsCon"] boolValue];

NSLog(@"IsPUstring...%d",IsPUstring);

NSLog(@"IsConstring...%d",IsConstring);

如果您想以NSString格式获取此值,那么您也可以使用以下代码..

NSLog(@"IsPUstring... %s", IsPUstring ? "true" : "false");
NSLog(@"IsConstring... %s", IsConstring ? "true" : "false");

答案 1 :(得分:0)

0和1 否和是。 JSON数据中的布尔对象将转换为NSNumber *,NSNumber方法[aNumber boolValue]将返回YES和NO。

现在,一个值为0,1或2的整数也将被转换为NSNumber *,对于这三个值[aNumber boolValue]将返回NO表示0,YES表示1,YES表示2(对于任何非零的数字,它将返回YES。

如果你真的想区分false / true和0/1:

NSNumber* isPU = jsonMain [@"IsPU"];

if (isPU == (id) kCFBooleanFalse) { ... } // isPU is false and not 0 or "0"
else if (isPU == (id) kCFBooleanTrue) { ... } // isPU is true and not 1 or "1"
else { ... } // isPU is something else.