I am developing iOS App. I get JSON data from PHP server in the following code.
NSURL *requestUrl = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:requestUrl];
NSHTTPURLResponse *httpResponse;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&httpResponse error:nil];
NSString *str= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSArray *array =[NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments|NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&e];
NSLog(@"str=%@",str);
NSLog(@"error=%@",e);
NSLog(@"error=%@",e)
says: "Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Badly formed object around character 3010.) UserInfo=0x15ed7f40 {NSDebugDescription=Badly formed object around character 3010.}".
NSLog(@"str=%@",str)
contains: "\343\201"
unexpectedly.
I think the problem is JSON data is not proper and above "\343\201"
causes this problem.
Could you tell me how to solve this problem?
A quick search of the error message indicates that the problem is due to malformed JSON data.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x984aeb0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
This would imply that you should allow fragments, which I can see is an option that you are already using. I suggest that you try to execute the JSON query in a browser and verify the JSON. You could copy it into a text editor to make sure that it is valid. Sharing that string (if it is not sensitive info) would allow us to further isolate the problem.
Some other people on SO are reporting similar issues, for example here. I suggest that you take the error message text "Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" and search SO for more clues on what caused their problems.
You might also want to try a different tact, where you set the options to nil (for purposes of a test) and then execute again and validate results.
UPDATE
OK, the complete error message is:
NSLog(@"error=%@",e) says "Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Badly formed object around character 3010.) UserInfo=0x15ed7f40 {NSDebugDescription=Badly formed object around character 3010.}".
Based on this you have some bad text in your JSON. Output to a text file and share if at all possible. A search of SO with these key words "JSON Badly formed object around character" reveals four answers of which this is one. Please review to see if those resolve your issue.