0

I'm using this lib to display emojis.

I want to get my data from a JSON, but for some reason the lib cannot convert a string from JSON. When I create my own string with the same data, e.g. '\uD83D\uDE18' it displays without any problem. emojis.posts[0].content == '\uD83D\uDE18' returned false

So my question is, what is the difference between a 'normal' string and a JSON string?

JSON

{
   "title": "l\\u2764 you\\uD83D\\uDE18"
}

...

var emojis = JSON.parse(jsonString);
console.log(emojis.title);

returns 'l\u2764 you\uD83D\uDE18'

Creating a JSON in JS

var emojis = JSON.parse('{"title": "l\\u2764 you\\uD83D\\uDE18"}');
console.log(emojis.title);

returns 'l❤ you'

Found the answer: https://stackoverflow.com/a/7885499/6216557

This is a unicode, escaped string. First the string was escaped, then encoded with unicode

Community
  • 1
  • 1
Jon
  • 55
  • 5
  • The difference between a normal string, and a JSON string, is that Douglas Crockpot invented the latter. – adeneo Apr 21 '16 at 20:23
  • Show some code, how do you parse your input and what format is it in? – Tamas Hegedus Apr 21 '16 at 20:25
  • `JSON.parse('"\\uD83D\\uDE18"')` works fine for me, are you certain you properly encoded your JSON source? – zzzzBov Apr 21 '16 at 20:26
  • Seems to work just fine -> https://jsfiddle.net/qk8dkmL9/ – adeneo Apr 21 '16 at 20:27
  • Your JSON is doubly escaped, it has two slashes in front of hex sequences instead of one. – Alexey Lebedev Apr 21 '16 at 20:38
  • console.log(unescape(emojis.title)); still returns 'l\u2764 you\uD83D\uDE18' – Jon Apr 21 '16 at 20:44
  • Did that answer really help you? escape/unescape are used for URL-encoding, that's a different kind of escaping in no way related to JavaScript (or JSON) string escaping. – Alexey Lebedev Apr 21 '16 at 22:27
  • Your second example works because the JSON object is embedded into a JavaScript string literal. JavaScript unwraps the first level of backslashes, and JSON.parse unwraps the second level. That is not the case in your first example. – Alexey Lebedev Apr 21 '16 at 22:34

0 Answers0