NSURLConnection : JSON text did not start with array or object and option to allow fragments not set

五迷三道 提交于 2019-12-22 01:33:49

问题


I get the following error from 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.}

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 :

{  
   "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.


回答1:


Set option value to NSJSONReadingAllowFragments instead of kNilOptions I have tested your JSON taking it in a local file

id json = [NSJSONSerialization JSONObjectWithData:contentOfLocalFile
                                                options:NSJSONReadingAllowFragments
                                                  error:&deserializingError];



回答2:


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.




回答3:


In addtion to Janmenjaya's answer i would like to add:-

I have faced this issue twice in different API's. The problem each time that i had was

  1. 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.
  2. Secondly , i was trying to hit an URL which had a word "video" in it for ex http://www.xyz/video123.com and 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.

Please ensure these cases also.



来源:https://stackoverflow.com/questions/27521547/nsurlconnection-json-text-did-not-start-with-array-or-object-and-option-to-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!