Phpjson_encode () function Chinese encoding garbled solution

Source: Internet
Author: User
When I use phpjson_encode (), if it is an English or a number, but it is not a problem to use Chinese, it is actually an unrecognized Chinese garbled text. The following describes how to solve the json_encode Chinese garbled text.

When I use php json_encode (), if it is an English or a number, but it is not a problem to use Chinese, it is actually an unrecognized Chinese garbled text. The following describes how to solve the json_encode Chinese garbled text.

Find a solution on the internet. the code is as follows:

  1. /* Process json_encode Chinese garbled characters */
  2. $ Data = array ('game' => 'ice-fire status', 'name' => 'Thorn Spirit ', 'Country' => 'ice-Frome status ', 'level' => 45 );
  3. Echo json_encode ($ data );
  4. Echo"
    ";
  5. $ NewData = array ();
  6. Foreach ($ data as $ key => $ value ){
  7. $ NewData [$ key] = urlencode ($ value );
  8. }
  9. Echo urldecode (json_encode ($ newData ));
  10. ?>

Later, I consulted someone and I could use base64 encoding. However, base64 encoding cannot be placed in URLs. Baidu explained this as follows:

The standard Base64 is not suitable for direct transmission in the URL, because the URL encoder will convert the "/" and "+" characters in the standard Base64 into the form of "% XX, these "%" signs need to be converted when they are stored in the database, because "%" has been used as a wildcard in ansi SQL.

However, my data is sent through POST, not in the HTTP head, but in the message-body, so it is not affected.

Json_encode can only accept UTF-8 data

For example, if 'signature' is changed to 'u80e5' after json_encode processing, the Chinese part of the final json is replaced with unicode encoding, what we need to solve is to convert the object to json and ensure that the Chinese character inside the object still appears in json as normal Chinese. now it seems that json_encode alone cannot achieve the goal.

My solution:Perform url encoding for the Chinese fields in the class, then perform json encoding (jsonencode) for the object, and finally url decoding (urldecode) json, that is, the final json, the Chinese character is still the Chinese character. the test code is as follows:

  1. Class myClass {
  2. Public $ item1 = 1;
  3. Public $ item2 = 'Chinese ';
  4. Function to_json (){
  5. // Url encoding to avoid converting Chinese to unicode using json_encode
  6. $ This-> item2 = urlencode ($ this-> item2 );
  7. $ Str_json = json_encode ($ this );
  8. // Url decoding. after converting json, all attributes are returned, so that the object attributes remain unchanged.
  9. $ This-> item2 = urldecode ($ this-> item2 );
  10. Return urldecode ($ str_json );
  11. }
  12. }
  13. $ C = new myClass ();
  14. Echo json_encode ($ c );
  15. Echo'
    ';
  16. Echo $ c-> to_json ();
  17. Echo'
    ';
  18. Echo json_encode ($ c );
  19. Echo'
    ';
  20. Echo json_encode ('authorization ');
  21. ?>

Program output result:

  1. {"Item1": 1, "item2": "u4e2du6587 "}
  2. {"Item1": 1, "item2": "Chinese "}
  3. {"Item1": 1, "item2": "u4e2du6587 "}
  4. "U80e5"

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.