0

To decode json to chinese: json_decode('"\ud83d\ude18\ud83d\ude18\ud83d\ude18\ud83d\ude18\u597d\u5bb6\u4f19\ud83d\ude0d\ud83d\ude0d\ud83d\ude0d"'); not works?

It works for chinese but not for smily

can you please give me any idea for it

Rizwan Gill
  • 2,193
  • 1
  • 17
  • 29
  • use the double quote `"` – Class Dec 02 '14 at 07:24
  • alrady tried with both single quotes and double but not works bro already tried by print json_decode("\ud83c\udf83\ud83c\udf83\ud83c\udf83"); print json_decode('\ud83c\udf83\ud83c\udf83\ud83c\udf83'); – Rizwan Gill Dec 02 '14 at 07:28
  • With special characters/sequences `\u` or `$` and such you should use `"` so PHP parser knows that there may contain items that need to be treated differently – Class Dec 02 '14 at 07:29
  • PHP6 is going to^^ have [`unicode_decode()`](http://php.chinaunix.net/manual/es/function.unicode-decode.php) for that. – mario Dec 02 '14 at 07:33
  • no, no, you have to have the double quotes _inside_ the single quotes -- the outer quotes are for the php language, the doublequotes must be contained inside the json string to be decoded. Experiment: check what json_encode("x") returns, and feed that back to json_decode() to get a feel for how it should work; _then_ try it with the breaking utf8. See the edit to my answer below for what it should look like. – Andras Dec 02 '14 at 07:35

1 Answers1

2

that's not a valid JSON string -- JSON strings must be inside double quotes

Edit: took the failing example above, wrapped the utf-8 in doublequotes, and it decoded:

var_dump(json_decode('"\ud83c\udf83\ud83c\udf83\ud83c\udf83"'));
string(12) ""

(I don't know what the glyphs should look like, I don't eve know if I have the right fonts installed, but the string decoded)

Andras
  • 2,995
  • 11
  • 17
  • 2
    Perhaps an example is called for. – Ignacio Vazquez-Abrams Dec 02 '14 at 07:29
  • it is valid json i obtain it just like echo json_encode('工作'); while i was encoding chinese to json now vice versa i wanrt to decode which not works for me. got the point? as it is obtained from json_encode function so it is valid json – Rizwan Gill Dec 02 '14 at 07:34
  • look carefully, the var_dump output of json_encode starts with "" and ends with "" -- two sets of quotes each `var_dump(json_encode('\ud83c\udf83')); string(16) ""\\ud83c\\udf83"" ` – Andras Dec 02 '14 at 07:39
  • IT only works in mozila firefox browser but chrome it is not working – Rizwan Gill Dec 05 '14 at 16:26