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在编码的时候已经转义了,addslashes和stripcslashes都是多此一举。
//PHP版本5.4以上:
json_encode($data,JSON_UNESCAPED_UNICODE);
//PHP版本5.2以上才有json_encode/json_decode系列函数。
//PHP版本5.2-5.3,可以利用PHP的urlencode/urldecode的组合使用,可以达到类似的保留中文的效果。
//示例如下:代码里的MyJsonEncode和MyJsonDecode函数可以保留中文。
//以上代码来自MyPHP开源函数库。
//GitHub开源地址:https://github.com/MoonLord-LM/MyPHP
//我的Blog:http://www.moonlord.cn
字符串直接写入表示的是正常的转义. 话说encode完的不就可以直接decode么,为什么还要做这种多余的动作……