Anurag Sharma Anurag Sharma - 1 month ago 10
JSON Question

How to solve JSON response could not be parsed issue?

It seems to be very critical issue, I have googled a lot about it but I didn't get the solution.

I am using:

[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse response, NSData jsonData, NSError *error)


This is what I am using to parse JSON data:

NSDictionary *jsonResponse =
[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];


and this is what I am getting:


JSON text did not start with array or object and option to allow
fragments not set.) UserInfo=0x16dd0320 {NSDebugDescription=JSON text
did not start with array or object and option to allow fragments not
set.


any help will be appreciated
Thanks in advance

Answer

This is a very common issue in Objective-c. Use this snippet in your request-

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

Hope this will help you.