How does NSJSONSerializer determine the encoding of the given data?

1k Views Asked by At

I'm using NSJSONSerializer to parse some json formatted data. The data contains some 'ü' and 'ö' and such. I find that it does return nil, when not beforehand converting the NSData to a NSString with NSASCIIStringEncoding and reconverting it back to NSData, so that 'ü' is converted to u and so on. The original format of the Data is ISO-8859-1. How ca i possibly advise the json parser to use this encoding?

1

There are 1 best solutions below

0
On BEST ANSWER

Per RFC 7159:

8.1. Character Encoding

JSON text SHALL be encoded in UTF-8, UTF-16, or UTF-32.

So if your data is encoded in ISO-8859-1 then it is not valid JSON.

Use NSString to convert it to one of the defined acceptable JSON forms:

NSString *string = [[NSString alloc] initWithData:dat encoding:NSISOLatin1StringEncoding];
NSData *utf8Data = [string dataUsingEncoding:NSUTF8StringEncoding];

// ... give utf8Data to NSJSONSerializer