iOS JSON Parse无法正常工作(返回null字典)

byt*_*yer 9 parsing json ios nsjsonserialization

我使用NSJSONSerialization's JSONObjectWithData:data options: error:来解析从服务器返回的JSON数据.

现在我使用的选项参数:NSJSONReadingAllowFragments.您可以在下面查看实际的JSON(我认为问题出在哪里).

我得到的错误信息是:

错误域= NSCocoaErrorDomain代码= 3840"操作无法完成.(Cocoa错误3840.)"(字符0周围的值无效.)UserInfo = 0x6895da0 {NSDebugDescription =字符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"]}}
Run Code Online (Sandbox Code Playgroud)

Dav*_*d H 12

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

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);
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:澄清一下,只需在您的JSON字典上运行上述内容 - 无法解析的字典.