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

假如想象 提交于 2019-11-28 12:27:17

"\ud83d\ude04" 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": "\ud83d\ude04" }
Emoji: 😄

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

Please follow following steps:

  • Convert Emoji characters to base64 and send to server using Json.
  • On server side save base64 in database without decode.
  • When you want to display Emoji on Application then retrieve same base64 data from server.
  • Decode retrieve string and display on app.
  • Your Emoji character will display properly.

Note: When you want to Show on webPage then Decode data when you display data on WebPage.

Our team fixed this problem by transfer utf data to server, saving them in mysql utf8mb4 codepage and receiving with base64. Server convert saved data to base64 on demand.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!