使用NSJSONSerialization解析JSON:错误3840 - 数据已损坏? [英] Parsing JSON with NSJSONSerialization: Error 3840 - Data Corrupted?
问题描述
我在这个问题上阅读了很多Q / As,但找不到适合我情况的答案。
我从我在PHP中创建的REST服务检索JSON响应。这是我的代码:
NSURLResponse * response = nil;
NSError * theError1 = nil;
NSError * theError2 = nil;
NSURL * webServiceUrl = [NSURL URLWithString:@http:// ...];
NSURLRequest * request = [NSURLRequest requestWithURL:webServiceUrl cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
NSData * theData = [NSURLConnection sendSynchronousRequest:request returningResponse:& response error:& theError1];
NSString * dataString = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
NSLog(@%@,dataString);
id json = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers错误:& theError2];
if(theError2!= nil)
NSLog(@%@,theError2);
当我在浏览器中调用REST调用时,我看到以下响应, XCode日志:
{
Name:REST Service,
Product: REST测试,
版本:1.0.0.0,
版权所有:2013测试公司
}
但是,当我执行上面的代码时,会创建并记录以下错误:
错误Domain = NSCocoaErrorDomain Code = 3840无法读取数据,因为它已损坏。 (字符3周围的值无效。)UserInfo = 0x100547430 {NSDebugDescription =字符3周围的值无效。}
我做错了什么?
确定,一如既往,检查实际数据,而不是字符串表示支付 - 感谢@Bavarious。 b
$ b
事实证明,负责创建JSON的PHP脚本都是UTF8 with BOM,所以PHP为每个涉及的脚本返回一个BOM。
一旦我将所有PHP文件更改为没有BOM的UTF8,一切似乎都很好 - 需要在MAC上进行测试。
对不起,中断,保持良好的工作。
(@ Bavarious:如果你想写一个答案,很高兴获得并接受它,因为你指出我的解决方案)。
现在可以解析JSON如预期。做一个精神笔记,总是仔细检查文本文件的编码。
I've read many Q/As on this problem but couldn't find an answer that fits my situation.
I retrieve a JSON response from a REST service I've created in PHP. This is my code:
NSURLResponse *response = nil;
NSError *theError1 = nil;
NSError *theError2 = nil;
NSURL *webServiceUrl = [NSURL URLWithString:@"http://..."];
NSURLRequest *request = [NSURLRequest requestWithURL:webServiceUrl cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
NSData *theData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&theError1];
NSString *dataString = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
NSLog(@"%@", dataString);
id json = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:&theError2];
if (theError2 != nil)
NSLog(@"%@", theError2);
When I invoke the REST call in the browser, I see the following response, which seems identical to what XCode logs:
{
"Name": "REST Service",
"Product": "REST Test",
"Version": "1.0.0.0",
"Copyright": "2013 Test Company"
}
When I execute above code, however, the following error is created and logged:
Error Domain=NSCocoaErrorDomain Code=3840 "The data couldn’t be read because it has been corrupted." (Invalid value around character 3.) UserInfo=0x100547430 {NSDebugDescription=Invalid value around character 3.}
What am I doing wrong?
OK, as always, checking the actual data instead of the string representation pays - thanks @Bavarious.
It turns out that the PHP script(s) in charge of creating the JSON were all "UTF8 with BOM", so PHP returned a BOM for every script involved.
Once I changed all the PHP files to "UTF8 without BOM" everything seems to be fine - need to test this on the MAC though.
Sorry to interrupt, keep up the good work.
(@Bavarious: If you'd like to write an answer, I'd be happy to upvote and accept it, as you pointed me to the solution).
Was able to parse the JSON now as expected. Making a mental note to always double-check the text file encoding.
这篇关于使用NSJSONSerialization解析JSON:错误3840 - 数据已损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!