Explain in detail the problems encountered in parsing boolean data in iOS development

  • 2021-09-20 21:39:35
  • OfStack

Problem description:

JSON data printed in Xcode:


{
 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 data returned by web page request:


{
"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"
}

Obviously, the manage field and the share field here are obviously data of type boolean. However, using the BOOL class to receive the data of these two fields,


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

The results are all YES, and in fact the value of the share field should be NO.

Solution:


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

After such processing, the obtained value of manage is YES, and the value of share is NO.


Related articles: