Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 544
Simon Avatar asked Nov 15 '11 20:11

Simon


1 Answers

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.

like image 85
fishinear Avatar answered Oct 23 '22 01:10

fishinear