iOS JSON Parse不工作(返回null字典) [英] iOS JSON Parse not working (returns null dictionary)

查看:887
本文介绍了iOS JSON Parse不工作(返回null字典)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NSJSONSerialization JSONObjectWithData:数据选项:错误:来解析从服务器返回的JSON数据。

I use the NSJSONSerialization's JSONObjectWithData:data options: error: to parse JSON data returned from a server.

现在我使用的options参数: NSJSONReadingAllowFragments 。你可以看下面看到实际的JSON(我相信问题所在)。

Now for the options parameter I use: NSJSONReadingAllowFragments. You can look below and see the actual JSON (where I believe the problem is).

我得到的错误信息是:


Error Domain = NSCocoaErrorDomain Code = 3840操作无法完成。(Cocoa error 3840.)(字符0周围的值无效。)UserInfo = 0x6895da0 {NSDebugDescription =字符0周围的值无效。}

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x6895da0 {NSDebugDescription=Invalid value around character 0.}

任何想法如何解决?

JSON =

{"name":"Johan Appleseed",
"email":"j.appleseed@emuze.co",
"phone":"+4121876003",
"accounts":{
    "facebook":[true,1125],
    "twitter":[false,null],
    "homepage":[true,"http:\/\/johnAplleseed.com\/index.html"]}}


推荐答案

可能你有一些你看不到的不可打印的角色。试试这个:

Probably you have some unprintable character that you cannot see. Try this:

NSData *jsonData = ...
const unsigned char *ptr = [data bytes];

for(int i=0; i<[data length]; ++i) {
  unsigned char c = *ptr++;
  NSLog(@"char=%c hex=%x", c, c);
}

验证您在开头或结尾没有不可打印的字符数据。

To verify you don't have unprintable characters at the beginning or end of the data.

编辑:澄清一下,只需在你的JSON字典上运行上面的代码 - 那个无法解析的字典。

to clarify, just run the above on your JSON dictionary - the one that fails to parse.

这篇关于iOS JSON Parse不工作(返回null字典)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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