当前位置 : 主页 > 手机开发 > ios >

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

来源:互联网 收集:自由互联 发布时间:2021-05-10
问题描述: Xcode中打印的JSON数据: { content = { createTime = 1462512975497; expiryDate = 1475137813; id = 204; intervalSeconds = 0; lastHgt = "63.689"; lastLat = "39.9621096"; lastLng = "116.3175201"; lastTime = 1462848844;mana

问题描述:

Xcode中打印的JSON数据:

{
 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数据:

{
"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类接收这两个字段的数据,

NSDictionary *content = [obj objectForKey:@"content"];
BOOL manage = [content objectForKey:@"manage"];
BOOL share = [content objectForKey:@"share"];

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

解决办法:

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

网友评论