Analysis of Json_encode Chinese coding problem in PHP Json_encode

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.