Json_encode转义问题

默认的json_code会把特殊字符进行转义,还会把中文转为Unicode编码形式
1、不转义中文
2、不转义特殊符号(斜杠)

<?php
$json = '{"name":"测试","gender":"male","demo":"/yian/"}';
echo $json;
echo "\n";
var_dump(json_decode($json));
echo "\n";
//默认的json_code是会对 / 转义成 \/ 的
echo json_encode(json_decode($json));
echo "\n";
//不转义斜杠
echo json_encode(json_decode($json),JSON_UNESCAPED_SLASHES);
echo "\n";
echo str_replace("\\/", "/",  json_encode(json_decode($json)));
echo "\n";
echo (json_encode(json_encode(json_decode($json))));
echo "\n";
//不转义中文
echo json_encode(json_decode($json), JSON_UNESCAPED_UNICODE); //必须PHP5.4+
echo "\n";
echo json_encode(json_encode(json_encode(json_decode($json))));

输出分别如下:
{"name":"测试","gender":"male","demo":"/yian/"}
object(stdClass)#1 (3) {
["name"]=>
string(6) "测试"
["gender"]=>
string(4) "male"
["demo"]=>
string(6) "/yian/"
}

{"name":"u6d4bu8bd5","gender":"male","demo":"/yian/"}
{"name":"u6d4bu8bd5","gender":"male","demo":"/yian/"}
{"name":"u6d4bu8bd5","gender":"male","demo":"/yian/"}
"{"name":"\u6d4b\u8bd5","gender":"male","demo":"\/yian\/"}"
{"name":"测试","gender":"male","demo":"/yian/"}
""{\"name\":\"\\u6d4b\\u8bd5\",\"gender\":\"male\",\"demo\":\"\\\/yian\\\/\"}""

添加新评论