iOS-NSJSONSerialization:无法将数据转换为字符周围的字符串 [英] iOS - NSJSONSerialization: Unable to convert data to string around character

查看:111
本文介绍了iOS-NSJSONSerialization:无法将数据转换为字符周围的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析JSON时遇到此错误:

I'm getting this error while parsing JSON:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unable to convert data to string around character 73053.) UserInfo=0x1d5d8250 {NSDebugDescription=Unable to convert data to string around character 73053.}

任何建议如何解决此问题?

Any suggestions how to fix this?

已添加
正如错误报告中所述,解析器无法通过位置73053处的字符,在我的位置中是ø JSON响应。据我所知,诸如Ø,Å,Æ等字符对于json解析器应该不是问题吗?

ADDED As it says in error report, the parser can't go through the character at position 73053, which is "ø" in my JSON response. As far as I know characters like Ø,Å,Æ etc. shouldn't be a problem for json parsers?

推荐答案

检查您正在解析的数据实际上是有效的JSON(而不仅仅是几乎 JSON)。当您使用无法解析为JSON的其他数据格式时,就会发生该错误。例如,参见:

Check that the data you're parsing is actually valid JSON (and not just 'nearly' JSON). That error is known to occur when you have a different data format that can't be parsed as JSON. See for example:

可可错误3840中的iOS 5 JSON解析结果

您的JSON中是否也有顶级容器?数组或字典。示例:

Do you have a top-level container in your JSON too? An array or dictionary. Example:

{ "response" : "Success" }

更新

JSON的默认编码为UTF-8。特殊/奇异字符对于UTF-8来说不是问题,但请确保您的服务器返回的内容正确编码为UTF-8。另外,您是否已做任何事情告诉JSON解释器使用其他编码?

JSON's default encoding is UTF-8. Special/exotic characters aren't a problem for UTF-8, but please ensure that your server is returning its content properly encoded as UTF-8. Also, have you done anything to tell your JSON interpretter to use a different encoding?

如果您的JSON来自网络服务,请将URL放入此页面以查看

If your JSON is coming from a web service, put the URL into this page to see what it has to see about the encoding:

http:/ /validator.w3.org/

这篇关于iOS-NSJSONSerialization:无法将数据转换为字符周围的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆