-1

我的回应:(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);

输出:

是PU字符串... 0

IsConstring... 1

我的问题:

原始标签值:

“IsCon”:真,“IsPU”:假

但是在 JSON 序列化之后它变成了 0 和 1 对吧?

有什么方法可以在这里获得我的原始值,例如 true 或 false ?

4

2 回答 2

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");
于 2013-07-23T11:47:17.300 回答
0

0 和 1NO 和 YES。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. 
于 2014-03-18T16:14:24.597 回答