时间:2021-07-01 10:21:17 帮助过:24人阅读
输出结果:
class myClass {
public $item1 = 1;
public $item2 = '中文';
function to_json() {
//url编码,避免json_encode将中文转为unicode
$this->item2 = urlencode($this->item2);
$str_json = json_encode($this);
//url解码,转完json后将各属性返回,确保对象属性不变
$this->item2 = urldecode($this->item2);
return urldecode($str_json);
}
}
$c = new myClass();
echo json_encode($c);
echo '
';
echo $c->to_json();
echo '
';
echo json_encode($c);
echo '
';
echo json_encode('胥');
?>
程序
{"item1":1,"item2":"\u4e2d\u6587"}
{"item1":1,"item2":"中文"}
{"item1":1,"item2":"\u4e2d\u6587"}
"\u80e5"
希望本文起到抛砖引玉的作用,收集大家更好的解决方法……!