来自服务器的 JSON 转换

Posted

技术标签:

【中文标题】来自服务器的 JSON 转换【英文标题】:JSON conversion from server 【发布时间】:2014-05-16 13:57:13 【问题描述】:

我正在开发一个使用 json 表示获取数据并使用 NSJSONSerialization 将其转换为基础对象的应用程序。

我的问题是从服务器获取数据后出现的。这是一个响应示例:

"groups":"15":"idgroupcat":"15","name":"\u05e7\u05d1\u05d5\u05e6\u05d4 1","电源":"9":"idmaincat":"9","名称":"\u05e8\u05d0\u05e9\u05d9 1","subs":"12":"idsubcat":"12","idcategory":"23","name":"\u05ea\u05ea 1","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0" ,"16":"idgroupcat":"16","name":"组 1","mains":"10":"idmaincat":"10","name":"main 1","subs":"13":"idsubcat":"13","idcategory":"24","name":"sub 1","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0" ,"11":"idmaincat":"11","name":"main 2","subs":"14":"idsubcat":"14","idcategory":"25","name":"sub 2","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0" ,"13":"idmaincat":"13","name":"main 2","subs":"16":"idsubcat":"16","idcategory":"26","name":"sub 2","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0" ,"14":"idmaincat":"14","name":"main 2","subs":"17":"idsubcat":"17","idcategory":"27","name":"sub 2","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0" ,"15":"idmaincat":"15","name":"main 2","subs":"18":"idsubcat":"18","idcategory":"28","name":"sub 2","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0", "23":"idsubcat":"23","idcategory":"31","name":"sub 8","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0", "24":"idsubcat":"24","idcategory":"32","name":"sub 8","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0" ,"17":"idgroupcat":"17","name":"组 2","mains":"19":"idmaincat":"19","name":"main 4","subs":"22":"idsubcat":"22","idcategory":"30","name":"sub 6","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0" ,"19":"idgroupcat":"19","name":"组 2","mains":"18":"idmaincat":"18","name":"main 3","subs":"21":"idsubcat":"21","idcategory":"29","name":"sub 3","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0" ,"20":"idgroupcat":"20","name":"组 2","mains":"20":"idmaincat":"20","name":"main 3","subs":"25":"idsubcat":"25","idcategory":"33","name":"sub 3","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0" ,"21":"idgroupcat":"21","name":"组 2","mains":"21":"idmaincat":"21","name":"main 3","subs":"26":"idsubcat":"26","idcategory":"34","name":"sub 3","title":"","workpicurl":"","numberofprofs":null,"numberofworks":null,"averageapplicationsperwork":null,"averagecloseprice":null,"averagewhen":"0"

一旦我尝试使用 NSJSONSerialization 转换它,它就会返回 null

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&err];

如果我使用该方法查看这是否是一个有效的 json 对象,该方法返回 NO。

我知道问题出在服务器响应中,但我不知道针对特定问题向程序员提供什么输入。服务器是用php写的

有什么建议吗? 谢谢,

【问题讨论】:

错误变量err返回什么错误? 错误很重要,它是有效的JSON:jsonlint.com "操作无法完成。(Cocoa 错误 3840.)" 这就是为什么 jsonlint 刚才对我来说很慢,所有的 SO 都在使用它。 :-) 查看导致 JSON 反序列化的几行代码也很有帮助,以证明 data1 确实是发布字符串的 NSData。 将 JSON 字符串放入数据很可能是个问题——很可能是因为使用了错误的字符串编码。需要查看更多代码。 【参考方案1】:

我认为问题在于 id 组 15,有一些未知字符(由我)未正确呈现。在调用输出方法之前尝试对服务器 php 文件进行此修复:

header ('Content-type: text/html; charset=utf-8');
echo json_encode($yourDataObject);

【讨论】:

【参考方案2】:

好的,在深入研究代码之后。

问题出现在服务器端的响应编码中。为了解决这个问题,将 utf-8 标头添加到响应中。这解决了问题,现在可以将字符串转换为基础对象。

【讨论】:

【参考方案3】:

来自 Zend Framework 1 的 Json 解码器可以与您提供的 JSON 一起工作:

    $phpNative = Zend_Json::decode($yourJSON, Zend_Json::TYPE_OBJECT);

我会复制粘贴到 $yourJSON

【讨论】:

以上是关于来自服务器的 JSON 转换的主要内容,如果未能解决你的问题,请参考以下文章

使用 JQuery 处理来自帖子的数据的正确方法

如何将大型 JSON 字符串转换为 JSON 对象?

来自json的响应字节数组

遍历来自 Alamofire 的 JSON 响应

JSON下

如何仅使用 Jackson 将 XML 转换为 JSON?