ios NSURLConnection :JSON 文本没有以数组或对象开头,并且没有设置允许片段的选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27521547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSURLConnection : JSON text did not start with array or object and option to allow fragments not set
提问by NITHIN S
I get the following error from NSURLConnection connectionDidFinishLoading
我从 NSURLConnection connectionDidFinishLoading 收到以下错误
"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=0x7b71dbb0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
“无法完成操作。(Cocoa 错误 3840。)”(JSON 文本未以数组或对象开头,并且未设置允许片段的选项。) UserInfo=0x7b71dbb0 {NSDebugDescription=JSON 文本未以数组或对象开头和允许未设置片段的选项。}
I used the following code :
我使用了以下代码:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;
id json = [NSJSONSerialization JSONObjectWithData:_urlData options:kNilOptions error:&error];
if (error) {
NSLog(@"Loading Error = %@",error);
}
}
The following is my json response :
以下是我的 json 响应:
{
"result":"success",
"details":[
{
"id":"11531",
"user_name":"",
"fullname":"aa",
"email_address":"aa",
"user_type":"a",
"server":"",
"server_email":"",
"server_password":"",
"password":"0cc175b9c0f1b6a831c399e269772661",
"clean_password":"a",
"gender":"",
"ceo_name":"",
"ceo_picture":"",
"ceo_contact":"",
"ceo_contact_pic":"",
"company_name":"a",
"freight_company_name":"",
"freight_company_email":"",
"company_url":"11531",
"company_keyword":"",
"company_description":"",
"address":"",
"province":"",
"postal_code":"",
"phone_number":"",
"fax_number":"",
"website":"",
"city":"",
"b_category":"",
"main_products":"",
"cellphone":"a",
"country":"0",
"states":"",
"company_status":"1",
"joindate":"0",
"varificationcode":"",
"activation_status":"1",
"new_email":"",
"email_activation_status":"",
"facebook_url":"",
"twitter_url":"",
"company_update_status":"0",
"last_access_date":"0000-00-00",
"ip_address":"",
"ip_block":"0",
"user_id":null,
"company_id":null,
"video_url":"",
"oauth_uid":"",
"oauth_provider":"",
"get_color":"",
"comp_name_size":"13",
"member_type":"",
"mark_status":"1",
"ip_address_intension":"",
"fbId":"",
"twitterId":"",
"profile_picture":"",
"device_token":""
}
]
}
I have tried all the solutions in stackOverflow but in vein.
我已经在 stackOverflow 中尝试了所有的解决方案,但在静脉中。
回答by Janmenjaya
Set option value to NSJSONReadingAllowFragments
instead of kNilOptions
I have tested your JSON taking it in a local file
将选项值设置为NSJSONReadingAllowFragments
而不是kNilOptions
我已经在本地文件中测试了您的 JSON
id json = [NSJSONSerialization JSONObjectWithData:contentOfLocalFile
options:NSJSONReadingAllowFragments
error:&deserializingError];
回答by gnasher729
NSLog the actual data that you are getting, not the string, and check the first bytes. JSONSerializer didn't find a { or a [ as the first character, so you probably have some zero bytes, or byte order marks, or some other invisible characters like that.
NSLog 记录您获得的实际数据,而不是字符串,并检查第一个字节。JSONSerializer 没有找到 { 或 [ 作为第一个字符,因此您可能有一些零字节、字节顺序标记或其他一些类似的不可见字符。
回答by Kunal Gupta
In addtion to Janmenjaya's answer i would like to add:-
除了 Janmenjaya 的回答,我想补充:-
I have faced this issue twice in different API's. The problem each time that i had was
我在不同的 API 中两次遇到过这个问题。每次我遇到的问题是
- First time the response that i was receiving was not in correct format. Remember the format must always start with a "[" or "{". So that was corrected from backend.
- Secondly , i was trying to hit an URL which had a word "video" in it for ex http://www.xyz/video123.comand websites related to name video have been blocked in our office. Therefore ensure that the network that you are using has no such restrictions. Postman will show you correct response but in devices or simulators you will face issues.
- 我第一次收到的响应格式不正确。请记住,格式必须始终以“[”或“{”开头。所以这是从后端纠正的。
- 其次,我试图点击一个包含“视频”字样的 URL,例如http://www.xyz/video123.com并且与名称视频相关的网站已在我们的办公室中被屏蔽。因此请确保您使用的网络没有此类限制。邮递员会向您显示正确的响应,但在设备或模拟器中您会遇到问题。
Please ensure these cases also.
也请确保这些情况。