复制代码 代码如下:
<?php
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 '<br/>';
echo $c->to_json();
echo '<br/>';
echo json_encode($c);
echo '<br/>';
echo json_encode('胥');
?>
复制代码 代码如下:
{"item1":1,"item2":"/u4e2d/u6587"}
{"item1":1,"item2":"中文"}
{"item1":1,"item2":"/u4e2d/u6587"}
"/u80e5"
新闻热点
疑难解答