iOS 5 JSON 解析结果 Cocoa 错误 3840
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11174130/
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
iOS 5 JSON Parsing Results in Cocoa Error 3840
提问by Gup3rSuR4c
I'm having a hard time parsing the below JSON string on iOS 5.
我很难在 iOS 5 上解析以下 JSON 字符串。
{"States": [{"Name": "Arizona","Cities": [{"Name": "Phoenix"}]},{"Name": "California","Cities": [{"Name": "Orange County"},{"Name": "Riverside"},{"Name": "San Diego"},{"Name": "San Francisco"}]},{"Name": "Nevada","Cities": [{"Name": "Las Vegas"}]}]}
And here's my code:
这是我的代码:
- (void) parseJson {
NSError *jsonError = nil;
NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Locations-JSON" ofType:@"rtf"]];
if (jsonData) {
NSDictionary *jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonError) {
NSLog(@"JSON Error: %@", [jsonError localizedDescription]);
return;
}
NSLog(@"%@", jsonObjects);
}
}
I keep getting this error:
我不断收到此错误:
JSON Error: The operation couldn't be completed. (Cocoa error 3840.)
JSON Error: The operation couldn't be completed. (Cocoa error 3840.)
I'd appreciate some help on this because I clearly and incapable of fixing this.
我很感激这方面的一些帮助,因为我显然无法解决这个问题。
采纳答案by Dave DeLong
One thing that strikes me as incorrect is this:
让我觉得不正确的一件事是:
[[NSBundle mainBundle] pathForResource:@"Locations-JSON" ofType:@"rtf"]
Your data is an RTF file?? It should be a txt
file (or any other sort of plain text file). RTF files usually contain text formatting data, like this:
你的数据是RTF文件??它应该是一个txt
文件(或任何其他类型的纯文本文件)。RTF 文件通常包含文本格式数据,如下所示:
{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf470
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
\f0\fs24 \cf0 \{"States": [\{"Name": "Arizona","Cities": [\{"Name": "Phoenix"\}]\},\{"Name": "California","Cities": [\{"Name": "Orange County"\},\{"Name": "Riverside"\},\{"Name": "San Diego"\},\{"Name": "San Francisco"\}]\},\{"Name": "Nevada","Cities": [\{"Name": "Las Vegas"\}]\}]\}}
When I read thatin as a data and try to parse it as JSON, I get the 3840 error you're seeing. That error's description says:
当我将其作为数据读入并尝试将其解析为 JSON 时,我收到了您看到的 3840 错误。该错误的描述说:
The data couldn't be read because it has been corrupted. (No string key for value in object around character 2.)
无法读取数据,因为它已损坏。(对象中字符 2 周围的值没有字符串键。)
So what it looks like to me is that you don't actually have JSON. You have RTF data.
所以在我看来,你实际上没有 JSON。你有 RTF 数据。
回答by nebitrams
I had hit a similar problem. My JSON parser works intermittently when I download the JSON data from a server. Did you get your JSON data from this function?
我遇到了类似的问题。当我从服务器下载 JSON 数据时,我的 JSON 解析器会间歇性地工作。您是否从该函数中获取了 JSON 数据?
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
The NSData returned from this function could be partial data. You need to appendData to an instance variable with type: NSMutableData. Then you process your JSON in another function as follows:
从这个函数返回的 NSData 可能是部分数据。您需要将数据附加到类型为 NSMutableData 的实例变量。然后您在另一个函数中处理您的 JSON,如下所示:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
Reading this article for the details. It works for me
阅读这篇文章了解详情。这个对我有用
回答by kraftydevil
I was able to troubleshoot my JSON 3840 error by converting the NSData
object to an NSString
:
通过将NSData
对象转换为 ,我能够解决我的 JSON 3840 错误NSString
:
NSError *error;
NSObject *object = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if (object == nil) {
NSString *serverResponse = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
NSLog(@"\n\nError:\n%@\n\nServer Response:\n%@\n\nCrash:", error.description, serverResponse);
[NSException raise:@"Invalid Data" format:@"Unable to process web server response."];
}
回答by James Roeiter
If you arrived here because of the JSON and not because of the RTF , please check out this answer : IOS JSON Deserialization failure - STIG/NSJSONSerializer
如果你是因为 JSON 而不是因为 RTF 而来到这里,请查看这个答案: IOS JSON Deserialization failure - STIG/NSJSONSerializer