4

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?

Oleg
  • 2,984
  • 8
  • 43
  • 71
  • Did you check the response string and can you show it – user1111278 Jan 14 '13 at 15:13
  • Check to make sure your response is valid JSON – propstm Jan 14 '13 at 15:15
  • I have checked my JSON on JSON validator, looks like it is valid – Oleg Jan 14 '13 at 15:17
  • Could you post your response? Googling that error is showing results of people who have valid JSON, but their response is improperly formed. ex. http://stackoverflow.com/questions/9282771/cocoa-error-3840-nsjsonserialization and http://stackoverflow.com/questions/9282771/cocoa-error-3840-nsjsonserialization – propstm Jan 14 '13 at 15:26
  • Maybe you should post the snippet of JSON surrounding the problem characters. – Hot Licks Jan 14 '13 at 17:34

4 Answers4

22

Yes, I have the same problem with encoding issue and got the above error. I got the NSData from server as encoding:NSISOLatin1StringEncoding. So I had to convert it to UTF8 before parsing it using NSJSONSerialization.

NSError *e = nil;
NSString *iso = [[NSString alloc] initWithData:d1 encoding:NSISOLatin1StringEncoding];
NSData *dutf8 = [iso dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:dutf8 options:NSJSONReadingMutableContainers error:&e];
karim
  • 15,408
  • 7
  • 58
  • 96
  • hello how are able to see which encoding is being sent back ? in your case it was NSISOlating1stringEncoding, how can I figure out for mine what is being sent back ? – isJulian00 Mar 26 '19 at 01:27
7

Switf 3

let responseStrInISOLatin = String(data: data, encoding: String.Encoding.isoLatin1)
guard let modifiedDataInUTF8Format = responseStrInISOLatin?.data(using: String.Encoding.utf8) else {
      print("could not convert data to UTF-8 format")
      return
 }
do {
    let responseJSONDict = try JSONSerialization.jsonObject(with: modifiedDataInUTF8Format)
} catch {
    print(error)
}
Chandra
  • 379
  • 3
  • 13
6

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/

Community
  • 1
  • 1
occulus
  • 16,959
  • 6
  • 53
  • 76
  • Yep, one needs to assure that the character encoding being sent is identical to the one expected by the parser. A conflict in DBCS encoding schemes is very likely the problem here. – Hot Licks Jan 14 '13 at 17:37
  • Accepting your answer with a little delay :-) Solved the problem by encoding an output string with UTF-8. – Oleg Jun 13 '13 at 07:12
  • Helps me a lot! Sometimes server side may callback us with GBK encoding thus may cause this problem.In my condition, I have noticed the data encoding is GBK, so I manually convert the encoding to NSUTF8. – lynulzy Dec 07 '17 at 09:45
3

Swift 5:

Yes, i got the same error while parsing JSON data.

Solution : You have to first convert response data into String and then convert that Sting to Data using UTF8 encoding before decoding.

let utf8Data = String(decoding: responseData, as: UTF8.self).data(using: .utf8)
halfelf
  • 9,737
  • 13
  • 54
  • 63
Vishal
  • 59
  • 4