For example: ' Xu ' has been json_encode processed and changed to ' \u80e5 ', and the Chinese part of the final JSON is replaced with Unicode encoding. All we have to do is convert the object to JSON and make sure that the Chinese inside the object still appears in the normal Chinese language in JSON, and now it seems that the use of json_encode is not a good idea.
My workaround: First of the class in the text snippet URL encoding (urlencode), and then the object is JSON encoded (Jsonencode), the last URL decoding (urldecode) JSON, that is, the final JSON, the Chinese is still the Chinese!
The test code is as follows:
Copy the Code code as follows:
Class MyClass {
Public $item 1 = 1;
Public $item 2 = ' Chinese ';
function To_json () {
URL encoding to prevent Json_encode from converting Chinese to Unicode
$this->item2 = UrlEncode ($this->item2);
$str _json = Json_encode ($this);
URL decoding, returning each property after the JSON is finished, ensuring that the object properties are not changed
$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 (' Xu ');
?>
Program Output Result:
Copy the Code code as follows:
{"Item1": 1, "item2": "\u4e2d\u6587"}
{"Item1": 1, "item2": "Chinese"}
{"Item1": 1, "item2": "\u4e2d\u6587"}
"\u80e5"
I hope this article will play a role, to collect all the better solutions ...!
The above describes the Json_encode PHP json_encode Chinese encoding problem analysis, including the json_encode aspect of the content, I hope that the PHP tutorial interested in a friend helpful.