Oleg Oleg - 19 days ago 6
iOS Question

iOS - NSJSONSerialization: Unable to convert data to string around character

I'm getting this error while parsing JSON:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unable to convert data to string around character 73053.) UserInfo=0x1d5d8250 {NSDebugDescription=Unable to convert data to string around character 73053.}


Any suggestions how to fix this?

ADDED
As it says in error report, the parser can't go through the character at position 73053, which is "ø" in my JSON response. As far as I know characters like Ø,Å,Æ etc. shouldn't be a problem for json parsers?

Answer

Check that the data you're parsing is actually valid JSON (and not just 'nearly' JSON). That error is known to occur when you have a different data format that can't be parsed as JSON. See for example:

iOS 5 JSON Parsing Results in Cocoa Error 3840

Do you have a top-level container in your JSON too? An array or dictionary. Example:

{ "response" : "Success" }

Update

JSON's default encoding is UTF-8. Special/exotic characters aren't a problem for UTF-8, but please ensure that your server is returning its content properly encoded as UTF-8. Also, have you done anything to tell your JSON interpretter to use a different encoding?

If your JSON is coming from a web service, put the URL into this page to see what it has to see about the encoding:

http://validator.w3.org/