Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

xcode - iOS 5 JSON Parsing Results in Cocoa Error 3840

I'm having a hard time parsing the below JSON string on iOS 5.

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

I'd appreciate some help on this because I clearly and incapable of fixing this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

{
tf1ansiansicpg1252cocoartf1138cocoasubrtf470
{fonttblf0fswissfcharset0 Helvetica;}
{colortbl;
ed255green255lue255;}
margl1440margr1440vieww10800viewh8400viewkind0
pardx720x1440x2160x2880x3600x4320x5040x5760x6480x7200x7920x8640pardirnatural

f0fs24 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 that in as a data and try to parse it as JSON, I get the 3840 error you're seeing. That error's description says:

The data couldn’t be read because it has been corrupted. (No string key for value in object around character 2.)

So what it looks like to me is that you don't actually have JSON. You have RTF data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...