Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: '\ud83d\ude04', and iPhone displays it as a square. However, if I return as XML, the Emoji Icon won't become unicode like this: '\ud83d\ude04', 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

like image 962
user2002692 Avatar asked Jan 23 '13 20:01

user2002692


2 Answers

"\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).

like image 60
Martin R Avatar answered Sep 20 '22 21:09

Martin R


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.

like image 36
Sandeep Agrawal Avatar answered Sep 19 '22 21:09

Sandeep Agrawal