solution for cocoa error 3840 NSJSONSerialization

499 Views Asked by At

Ia m getting an cocoa error 3840 when I am trying to serialize the JSON response and I am getting the following error message in the console "JSON text did not start with array or object and option to allow fragments not set." But i happened to check the response in chrome client and I get the correct response. And when I copy pasted the response in JSONValidator, I got it as a valid JSON. But in my iOS app I am somehow not able to get the response.

Is there a way to fix it from iOS code? I tried all possible solutions from internet.

NSError *error = nil;
NSMutableArray *jsons = [NSJSONSerialization JSONObjectWithData:theResponseData options:NSJSONReadingAllowFragments error:&error];
NSLog(@"connectionDidFinishLoading %@", jsons);

I tried MutableContainers, MutableLeaves and kNil in options of serialization. Still I am not able to fetch the data.

1

There are 1 best solutions below

2
On

Try this - hope it will work

if ([operation isKindOfClass:[AFJSONRequestOperation class]] && [operation respondsToSelector:@selector(setJSONReadingOptions:)]) 
{    
    ((AFJSONRequestOperation *)operation).JSONReadingOptions = NSJSONReadingAllowFragments;
}

[httpClient enqueueHTTPRequestOperation:operation];

If it doesn't work then

[{"Firstname":"Johnny"}],

This is the only sample content your Web response should contain.