SoFunction
Updated on 2025-04-07

Detailed explanation of the problems encountered in parsing boolean-type data in JSON in iOS development

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

It is obvious that the manage field and share field here are obviously boolean type data. However, use 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 obtained 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 this processing, the obtained manage value is YES and the share value is NO.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.