iOS 5 JSON Parsing Results in Cocoa Error 3840

60,531

Solution 1

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:

{\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 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.

Solution 2

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?

- (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:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

Reading this article for the details. It works for me

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

Solution 3

I was able to troubleshoot my JSON 3840 error by converting the NSData object to an 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."];
}

Solution 4

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

Share:
60,531
Gup3rSuR4c
Author by

Gup3rSuR4c

Updated on July 16, 2022

Comments

  • Gup3rSuR4c
    Gup3rSuR4c almost 2 years

    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.

  • Gup3rSuR4c
    Gup3rSuR4c almost 12 years
    Yes, you're right, that was it. Man, I feel really stupid right now... Thanks for the help though.
  • jamil ahmed
    jamil ahmed over 10 years
    I got this error from just having invalid JSON (trailing ; at the end of JSON object definition). Generic advice may be to verify your JSON using any of the many JSON validator webpages out there.
  • SleepsOnNewspapers
    SleepsOnNewspapers about 9 years
    this helped me. do you have any idea what this response could mean @kraftydevil? The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application
  • kraftydevil
    kraftydevil about 9 years
    @hsavit1 My knowledge stops right at the native space. Your error is definitely an issue with the server / web service but it's over my head.