For example: 'Xu' becomes 'u80e5' after json_encode processing, and the Chinese part of the final json is replaced with unicode encoding. What we have to solve is to convert the object into json and ensure that the Chinese inside the object still appears as normal Chinese in json. Now it seems that only using json_encode cannot achieve the goal.
My solution: first url-encode the Chinese field in the class (urlencode), then json-encode the object (jsonencode), and finally url-decode (urldecode) the json, which is the final json, and the Chinese inside is still the same It's the Chinese one!
The test code is as follows:
Copy code The code is as follows:
class myClass {
public $item1 = 1;
public $item2 = 'Chinese';
function to_json() {
//url encoding, avoid json_encode converting Chinese to unicode
$this-> item2 = urlencode($this->item2);
$str_json = json_encode($this);
//URL decoding, return each attribute after converting to json, ensuring that the object attributes remain unchanged
$ 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('胥');
?>
Program output result:
Copy code The code is as follows:
{"item1":1,"item2":"u4e2du6587"}
{"item1 ":1,"item2":"中文"}
{"item1":1,"item2":"u4e2du6587"}
"u80e5"
I hope this article will serve as a starting point. The role of collecting better solutions from everyone...!
http://www.bkjia.com/PHPjc/324250.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324250.htmlTechArticleFor example: '襥' becomes 'u80e5' after json_encode processing, and the Chinese part of the final json is replaced by unicode encoding. What we have to solve is to convert the object to json and ensure the internal content of the object...