详解iOS开发中解析JSON中的boolean类型的数据遇到的问题

时间:2022-09-20 11:30:54

问题描述:

Xcode中打印的JSON数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
 content =  {
  createTime = 1462512975497;
  expiryDate = 1475137813;
  id = 204;
  intervalSeconds = 0;
  lastHgt = "63.689";
  lastLat = "39.9621096";
  lastLng = "116.3175201";
  lastTime = 1462848844;
manage = 1;
  nickName = "6ZOD6ZObNzM=";
share = 0;
  tname = 3233470E36343434FF726D73;
 };
 state = success;
}

网页请求返回的JSON数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"content":{
"id":203,
"createTime":1462755844018,
"share":false,
"lastHgt":63.689,
"intervalSeconds":0,
"nickName":"6ZOD6ZObNzM=",
"expiryDate":"1475137813",
"tname":"3233470E36343434FF726D73",
"lastTime":1462848844,
"lastLng":116.3175201,
"manage":true,"lastLat":39.9621096},
"state":"success"
}

很明显,这里manage字段和share字段明显是boolean类型的数据。但是,用BOOL类接收这两个字段的数据,

?
1
2
3
NSDictionary *content = [obj objectForKey:@"content"];
BOOL manage = [content objectForKey:@"manage"];
BOOL share = [content objectForKey:@"share"];

得到的结果都为YES,实际上share字段的值应该为NO。

解决办法:

?
1
2
BOOL manage = [[content objectForKey:@"manage"] boolValue];
BOOL share = [[content objectForKey:@"share"] boolValue];

经过这样处理后,得到的manage的值为YES,share的值为NO。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/jinrui_w/article/details/51371462