echo $res=addslashes(json_encode(array('content'=>'中文')));//{\"content\":\"\\u4e2d\\u6587\"}echo stripcslashes($res);//{"content":"\u4e2d\u6587"}直接echo stripcslashes( '{\"content\":\"\\u4e2d\\u6587\"}');//{"content":"u4e2du6587"}\不见了???不能用json_decode()解码了
Reply content:
echo $res=addslashes(json_encode(array('content'=>'中文')));//{\"content\":\"\\u4e2d\\u6587\"}echo stripcslashes($res);//{"content":"\u4e2d\u6587"}直接echo stripcslashes( '{\"content\":\"\\u4e2d\\u6587\"}');//{"content":"u4e2du6587"}\不见了???不能用json_decode()解码了
JSON is escaped when encoded, addslashes and stripcslashes are superfluous.
PHP version 5.4 or higher:
Json_encode ($data, Json_unescaped_unicode);
The Json_encode/json_decode series function is available in
//php version 5.2.
//php version 5.2-5.3, you can use the combination of PHP urlencode/urldecode, you can achieve a similar effect of retaining Chinese. The
///example is as follows: the Myjsonencode and Myjsondecode functions in the code can be reserved in Chinese.
The array conversion is reserved for the Chinese JSON string function Myjsonencode ($data) {return UrlDecode (Json_encode (Myurlencode ($data))); Requires PHP version 5.4 or more://return Json_encode ($data, Json_unescaped_unicode);} A JSON string that retains Chinese is converted to an array function Myjsondecode ($data) {$data = UrlEncode ($data); $data = Str_replace ("%7b", ' {', $data); $data = Str_replace ("%7d", '} ', $data); $data = Str_replace ("%5b", ' [', $data); $data = Str_replace ("%5d", '] ', $data); $data = Str_replace ("%3a", ': ', $data); $data = Str_replace ("%2c", ', ', $data); $data = Str_replace ("%22", ' "', $data); Return Myurldecode (Json_decode ($data, True));} The custom URL-encoded function Myurlencode ($data) {//Can be URL-encoded on an associative array and handle newline characters//internal recursive calls//for Myjsonencode function calls if (!is_array ($ Data) {$data = Str_replace ("\ r", ' \ R ', $data); $data = Str_replace ("\ n", ' \ n ', $data); $data = UrlEncode ($data); } else {foreach ($data as $key = + $value) {$data [Myurlencode ($key)] = Myurlencode ($value); if (string) Myurlencode ($key)!= = (string) $key) {unset ($data [$key]); }}} return $data;} Custom URL decoding function Myurldecode ($data) {//can URL decode an associative array and handle newline characters//internal recursive call//for Myjsondecode function call if (!is_array ($ Data) {$data = UrlDecode ($data); $data = Str_replace (' \ r ', "\ R", $data); $data = Str_replace (' \ n ', "\ n", $data); } else {foreach ($data as $key = + $value) {$data [Myurldecode ($key)] = Myurldecode ($value); if (string) Myurldecode ($key)!== (string) $key) {unset ($data [$key]); }}} return $data;}
The above code comes from the myphp Open Source Library.
GitHub Open Source Address: Https://github.com/MoonLord-LM/MyPHP
My blog:http://www.moonlord.cn.
A string direct write represents a normal escape. Say encode end of not can direct decode, why still do this extra action ...