Ios NSURLConnection:JSON文本未以数组或对象开头,并且未设置允许片段的选项
我从NSURLConnection ConnectionIDFinishLoading中得到以下错误 “操作无法完成。(Cocoa错误3840。)”(JSON文本 未以数组或对象开头,并且选项不允许碎片 set.)UserInfo=0x7b71dbb0{NSDebugDescription=JSON文本未启动 使用数组或对象以及允许不设置片段的选项。} 我使用了以下代码:Ios NSURLConnection:JSON文本未以数组或对象开头,并且未设置允许片段的选项,ios,objective-c,json,xcode,nsurlconnection,Ios,Objective C,Json,Xcode,Nsurlconnection,我从NSURLConnection ConnectionIDFinishLoading中得到以下错误 “操作无法完成。(Cocoa错误3840。)”(JSON文本 未以数组或对象开头,并且选项不允许碎片 set.)UserInfo=0x7b71dbb0{NSDebugDescription=JSON文本未启动 使用数组或对象以及允许不设置片段的选项。} 我使用了以下代码: -(void)connectionDidFinishLoading:(NSURLConnection *)connectio
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;
id json = [NSJSONSerialization JSONObjectWithData:_urlData options:kNilOptions error:&error];
if (error) {
NSLog(@"Loading Error = %@",error);
}
}
以下是我的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":""
}
]
}
我在stackOverflow中尝试了所有的解决方案,但都是在vein中尝试的。n记录您获得的实际数据,而不是字符串,并检查第一个字节。JSONSerializer没有找到{或[作为第一个字符,因此您可能有一些零字节、字节顺序标记或其他类似的不可见字符。将选项值设置为
NSJSONReadingAllowFragments
,而不是kNilOptions
我已经在本地文件中测试了JSON
id json = [NSJSONSerialization JSONObjectWithData:contentOfLocalFile
options:NSJSONReadingAllowFragments
error:&deserializingError];
除了Janmenjaya的回答,我想补充:- 我曾在不同的API中两次遇到过这个问题。每次遇到的问题都是
请同时确保这些情况。您是否尝试设置NSJSONReadingAllowFragments选项?顺便问一下。您确定您的_urlData在此位置完全包含您的JSON吗?我想您是通过NSURLConnections协议方法添加它。只需打印[[NSString ALOC]initWithData:\u urlData encoding:NSUTF8StringEncoding]您会看到。@Krivoblotsky:这是一个可读的鲱鱼。很明显,这是完整的JSON,而不是片段。这个问题解决了吗?尝试过这个,但它导致了一个更具体的错误:
(Cocoa错误3840.)(字符0周围的值无效。)
您的json格式似乎有误。请在联机json中检查您的jsonparser@MikeS,我认为响应是字符串格式的,请尝试以下操作:[NSString stringWithUTF8String:[数据字节]]我也遇到了同样的问题