NSJSONSerialization and Unicode will not play well together

I come to an application that talks to the server node using sockets and JSON, and since iOS 5 has its own NSJSONSerialization, I thought it could be expensive. I used to use a lightweight library.

Anyway, after switching to NSJSONSerialization, I started to encounter an encoding problem, before changing the character Γ– I would send it nicely to the server and back, still being Γ– , but now NSJSONSerialization leaves Γ– still in char unicode i.e. \U00f6 .

Reading the documentation states that JSON objects are converted to UTF8 by default. And when I convert the response from the server to a simple NSString, Γ– displayed as expected, but it is still in JSON, of course.

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

Thanks Simon

+6
source share
2 answers

NSLog calls up a description of its arguments, which print 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, it is likely that your JSON decoding was done perfectly.

+3
source

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 JSON, I recommend using https://github.com/johnezang/JSONKit

0
source

Source: https://habr.com/ru/post/901564/


All Articles