详解iOS开发中解析JSON中的boolean类型的数据遇到的问题
程序员文章站
2023-12-22 09:44:52
问题描述:
xcode中打印的json数据:
{
content = {
createtime = 1462512975497;
ex...
问题描述:
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。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。