json解析时出错

Posted

技术标签:

【中文标题】json解析时出错【英文标题】:getting error during json parsing 【发布时间】:2013-02-19 07:59:19 【问题描述】:

我在解析 json 时出错。当我尝试使用浏览器发送参数时,它会向我发送记录存储成功的成功消息。但是当我通过 iphone 发送表单参数时,我什么也得不到,在数组中我得到 NULL 值。这是 json 解析代码。

  NSString *string1 = [[NSString alloc]initWithFormat:@"http://195.78.76.34/bedspace/mobileapi.php?action=save_room_info&Area=%@&Address=%@&Living_room=%@&Amenties=%@&Type_of_cost=%@&cost_of_room=%@&Security_deposit=%@&STLC=%@&Days_available=%@&Bill_included=%@&Broadband_available=%@&Exc_Smoke=%@&Exc_gender=%@&Exc_pets=%@&Exc_age=%@&Exc_language=%@&Exc_nationality=%@&Exc_Sexorientition=%@&Exc_intrest=%@&Pre_smoke=%@&Pre_gender=%@&Pre_occupation=%@&Pre_pets=%@&Pre_min_age=%@&Pre_max_age=%@&Pre_nationaltiy=%@&Pre_Language=%@&Pre_Sexorientition=%@&Misc=%@&Title=%@&Description=%@&Your_name=%@&Your_email=%@&Email_alert=%@&No_of_rooms=%@&Country=%@&City=%@&State=%@&Size_of_property=%@&Type_of_property=%@&Occupets_property=%@&My_property_status=%@&Amenties_two=%@&Size_of_room=%@&Refrence_required=%@",area,address,livingRoomVal,amenties,typeOfCost,costOfRoom,securityDeposit,STLC,daysAvailable,billsIncluded,broadbandAvailable,eSmoke,eGender,ePets,eAge,eLanguage,eNationality,eSexOrientation,eInterest,pSmoke,pGender,pOccupation,pPets,pMinAge,pMaxAge,pNationality,pLanguage,pSexOrientation,misc,title,description,yourName,yourEmail,emailAlerts,noOfRooms,country,city,state,sizeOfProperty,typeOfProperty,occupentsOfProperty,myPropertyStatus,amentiesTwo,sizeOfRoom,refrenceRequired];
    NSLog(@"string 1 value %@",string1);
    NSURL *urlRequest = [NSURL URLWithString:string1];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:urlRequest];
   NSData *dataResponce = [NSURLConnection sendSynchronousRequest:request returningResponse:Nil error:Nil];

    NSError *jsonParsingError = nil;
    NSArray *publicTimeline = [NSJSONSerialization JSONObjectWithData:dataResponce options:0 error:&jsonParsingError]; 
 NSLog(@"get values %@",publicTimeline);

我不知道问题是什么。我也使用了SBJsonParser,但我在数组中什么也没有,之后我使用了 NSJSONSerialization,同样的问题仍然发生,我在 NSData 和 NSArray 中什么也没有。

【问题讨论】:

显示什么错误日志? 尝试使用 NSMutableURLRequest 并设置 HTTP 方法 POST,这可能对你有帮助 参数值是多少? 兄弟,我使用相同的代码来存储注册表单记录并成功存储,我已经完成 json 解析 100 次以将记录存储在数据库中并从数据库中获取记录并成功获取我没有知道为什么我会出错 在 nslog 上,我在 nsarray 中获得了 null 值,并且我记录了 nsdata,它也返回 null 【参考方案1】:

你不能用参数填充URL并且仍然期望它是合法的,所以你需要在使用前对URL进行URL编码:

NSURL *urlRequest = [NSURL URLWithString:[string1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

【讨论】:

以上是关于json解析时出错的主要内容,如果未能解决你的问题,请参考以下文章

解析 JSON 时出错

快速解析 JSON 并在数组中循环时出错

使用 Spotify API 时“解析 JSON 时出错”

使用换行符解析 json 时出错

解析 JSON 响应时出错 Cocoa 错误 3840

在 iOS 中使用数组解析 JSON 时出错