5

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

user2002692
  • 971
  • 2
  • 17
  • 34

3 Answers3

11

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

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • This solves my problem and it works perfectly. The way I parse the json response is: initWithData:jsonData encoding:NSUTF8StringEncoding; I guess since the unicode for Emoji is not utf8, I should not use utf8Encoding to validate it. However, initWithData:jsonData encoding:NSUTF16StringEncoding does not seem to work either. Any ideas? Thanks – user2002692 Jan 23 '13 at 22:45
  • 2
    @user2002692: `initWithData` converts `NSData` to `NSString` and does not know anything about JSON and therefore does not convert "\ud83d\ude04". – Martin R Jan 24 '13 at 07:47
  • How do you do this in swift? – BCLtd Feb 05 '16 at 14:01
  • @MartinR How did you get the unicode escape sequence from the Emoji? Is there a tool? – Isuru Jun 17 '16 at 11:07
  • @Isuru: I don't remember how I did it. I think in earlier OS X versions, the "Character Viewer" showed the surrogate pair for characters outside of the basic multilingual plane, but I cannot find that anymore in the "Emoji & Symbols" view in the current OS X. – However, it is easy with Swift: `print("".utf16.map { String(format: "%04x", $0) } )` – Martin R Jun 17 '16 at 11:38
  • @MartinR Thank you. I will try that. – Isuru Jun 17 '16 at 11:39
  • 1
    @Isuru you can find complete list of emoji in unicode escape sequence [here](https://github.com/shanraisshan/EmojiCodeSheet) – shanraisshan Feb 14 '17 at 05:03
8

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.

Sandeep Agrawal
  • 425
  • 3
  • 10
0

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.

Daniil Chuiko
  • 866
  • 8
  • 7