2

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?

Stunner
  • 12,025
  • 12
  • 86
  • 145
supermonkey
  • 631
  • 11
  • 25
  • 1
    Why are you converting from data to string and back to data? That just costs time, battery life, memory, and serves no purpose whatsoever. Show us the whole string that seems to cause the problem. If you receive bad JSON data, that needs to be fixed on the server. On the other hand, this may just by UTF-8 with non-ASCII data and is just normal. – gnasher729 Aug 27 '14 at 15:10
  • You're ignoring errors when you retrieve the data with NSURLConnection - have you checked if there's a problem there? Are you certain the data you get from the server is UTF-8? – thegrinner Aug 27 '14 at 15:11
  • -1 for not quoting the entire error message. – Hot Licks Aug 27 '14 at 15:19
  • Updated entire error message. – supermonkey Aug 27 '14 at 15:30

2 Answers2

3

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.

Stunner
  • 12,025
  • 12
  • 86
  • 145
Tommie C.
  • 12,895
  • 5
  • 82
  • 100
  • Thank you for your long reply. Referring to your advice, I can solve my problem. My problem is that above str contains double quotation. This causes inappropriate JSON string. – supermonkey Aug 27 '14 at 16:38
0

"\343\201" is just a Hiragana character. Absolutely normal and no problem from JSON. Post the whole string that you think causes the problem.

gnasher729
  • 51,477
  • 5
  • 75
  • 98