JSON文本不以数组或对象和选项开头,以允许未设置片段 [英] JSON text did not start with array or object and option to allow fragments not set

查看:537
本文介绍了JSON文本不以数组或对象和选项开头,以允许未设置片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从服务器发送这个json响应,请求我的IOS 7应用程序。

I'm sending this json response from server for a request to my IOS 7 app.

{
 "root": {
    "success": "1",
    "message": "Successfully retrieved data.",
    "data": {
        "records": [
            {
                "receipt_key": "xxxxxxxx",
                "receipt_id": "xxxxxxxx",
                "store_name": "xxxxxx",
                "amount": "xxxx",
                "date_purchase": "xxxxxxxx",
                "is_processed": "x",
                "created_on": "xxxxxxxx",
                "modified_on": "xxxxxxxx",
                "modified_on_millis": "xxxxxxxx",
                "user_folder": "xxxxxxxx",
                "category_id": "xxxxxxxx",
                "is_deleted": "x",
                "currency_id": "xxxxxxxx"
            }
        ]
    }
}
}

我使用以下代码进行解析上面的json到NSDictionary对象。

I use the following code for parsing the above json to NSDictionary object.

 NSMutableDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

但我在上面的代码中收到此错误。

But I'm getting this error on the above code.


错误域= NSCocoaErrorDomain Code = 3840操作不能完成
。(可可错误3840.)( JSON文本不是以数组或
对象和选项开头,以允许未设置片段。)UserInfo = 0x8a8a700
{NSDebugDescription = JSON文本不是以数组或对象开头而是
选项允许片段没有设置。}

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8a8a700 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}


推荐答案

我在从php中输入一个Feed时遇到了同样的错误页。就像您遇到的那样,生成的json字符串通过了视觉检查,但无法序列化。我怀疑是在某个地方有一个隐藏的字符,所以我将每个字符转换为其unicode十进制等值并检查结果:

I ran into the same error when consuming a feed from a php page. Just as you encountered, the resulting json string passed a visual inspection, but would fail to serialize. My suspicion was that there was a hidden character somewhere the feed, so I converted each character to its unicode decimal equivalent and examined the results:

NSString *feedStr = [[NSString alloc] initWithData:feedData encoding:NSUTF8StringEncoding];
for(int i=0; i<[feedStr length]; ++i)
{
    unichar c = [feedStr characterAtIndex:i];
    NSLog(@"decimal char %d", c);
}

我发现在第一个角色之前和最后一个角色之后是角色#65279 。快速谷歌搜索后,我发现这个字符是什么? 65279 ,其中标识为字节顺序标记

I found that before the first character and after the last was the character #65279. After a quick google search I found What is this char? 65279, where this was identified as a byte order mark.

在我的情况下,我能够通过打开并保存所有包含的php文件来修复此问题,使用文本编辑器提供了使用在UTF-8中编码的选项没有BOM编码。有关php端的更多信息,请参阅如何避免在php中回显字符65279?

In my case, I was able to fix this at the source by opening and saving all included php files, using a text editor that provided an option to use the "Encode in UTF-8 without BOM" encoding. For more info on the php side, see How to avoid echoing character 65279 in php?

这篇关于JSON文本不以数组或对象和选项开头,以允许未设置片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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