Simon
Simon

Reputation: 1076

NSJSONSerialization and Unicode, won't play nicely together

I'm in the making of an app that talks to a nodejs-server using sockets and JSON, and since iOS 5 has it's own NSJSONSerialization I thought that's may be the road to follow. Before, I used the lightweight yail library.

Anyway, after the migration to NSJSONSerialization I started to encounter problem with the encoding, before the change, the character Ö would send nicely to the server and back, still being Ö, but now, NSJSONSerialization leaves Ö still in unicode char i.e. \U00f6.

Reading the documentation says that JSON objects is converted to UTF8 by default. And when I convert the response from the server to a simple NSString, the Ö shows up just as expected, but it's still in JSON of course.

Please help me with your thoughts and so, should I return to yail or use the built-in NSJSONSerialization?

Thank you, Simon

Upvotes: 6

Views: 2225

Answers (2)

fishinear
fishinear

Reputation: 6336

NSLog calls description on its arguments, which happen to print the Unicode code instead of the character itself. Try for example:

NSLog(@"%@", [NSDictionary dictionaryWithObject:@"ö" forKey:@"hello"]);

And you will see that it prints

{
    hello = "\U00f6";
}

So, chances are, that your JSON decoding has been done fine.

Upvotes: 3

Jeremie D
Jeremie D

Reputation: 4204

JSONSerialization can only be used with certain types: NSString, NSNumber, NSArray, NSDictionary, or NSNull only by default... if you need to use other objects in your JSON, I recommend using https://github.com/johnezang/JSONKit

Upvotes: 0

Related Questions