Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

encode - IOS cannot decode emoji unicode in json format correctly, and Emoji icons are displayed as squares

I am working on an iPhone app which allows people to send messages with Emoji icons. I saved the icon in Mysql with charset utf8mb4 and collation utf8mb4_unicode_ci, and all the emoji icons is saved correctly in my database. However, when I return json back to the client (php json_encode), the Emoji is encoded as something like this: 'ud83dude04', and iPhone displays it as a square. However, if I return as XML, the Emoji Icon won't become unicode like this: 'ud83dude04', it will just be the icon.

I am wondering if this is an issue on my server, or on my client. If it is my client, how can object c decode it correctly.

Can someone please help?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"ud83dude04" is the JSON Unicode escape sequence for U+D83D U+DE04, which is the "surrogate pair" for the Unicode U+1F604 (SMILING FACE WITH OPEN MOUTH AND SMILING EYES).

But NSJSONSerialization decodes this correctly, as can be seen in the following example:

const char *jsonString = "{ "emoji": "\ud83d\ude04" }";
NSLog(@"JSON: %s", jsonString);
NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)];
NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.myLabel.text = [jsonDict objectForKey:@"emoji"];
NSLog(@"Emoji: %@", self.myLabel.text);

Output:

JSON: { "emoji": "ud83dude04" }
Emoji: ??

and the Emoji symbol is also displayed correctly (tested with iPhone device and Simulator).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...