php中json_encode中文编码问题
1 <?php
2 class myClass {
3 public$item1=1;
4 public$item2='中文';
5
6 function to_json() {
7 //url编码,避免json_encode将中文转为unicode
8 $this->item2 =urlencode($this->item2);
9 $str_json= json_encode($this);
10 //url解码,转完json后将各属性返回,确保对象属性不变
11 $this->item2 =urldecode($this->item2);
12 returnurldecode($str_json);
13 }
14 }
15
16 $c=new myClass();
17 echo json_encode($c);
18 echo'<br/>';
19 echo$c->to_json();
20 echo'<br/>';
21 echo json_encode($c);
22 echo'<br/>';
23 echo json_encode('胥');
24 ?>
程序输出结果:
{"item1":1,"item2":"\u4e2d\u6587"}
{"item1":1,"item2":"中文"}
{"item1":1,"item2":"\u4e2d\u6587"}
"\u80e5"