I have some json I need to decode, alter and then encode without messing up any characters.
If I have a unicode character in a json string it will not decode. I'm not sure why since json.org says a string can contain: any-Unicode-character- except-"-or-\-or- control-character
. But it doesn't work in python either.
{"Tag":"Odómetro"}
I can use utf8_encode which will allow the string to be decoded with json_decode, however the character gets mangled into something else. This is the result from a print_r of the result array. Two characters.
[Tag] => Odómetro
当我再次编码数组时,字符转义为ascii,根据json规范,这是正确的:
"Tag"=>"Od\u00f3metro"
有什么办法可以让我逃脱吗?json_encode没有提供这样的选项,utf8_encode似乎也不起作用.
Edit我看到json_encode有一个unscaped_unicode选项.然而,它并没有像预期的那样工作.哦,该死,它只在PHP5.4上.我将不得不使用一些正则表达式,因为我只有5.3.
$json = json_encode($array, JSON_UNESCAPED_UNICODE);
Warning: json_encode() expects parameter 2 to be long, string ...