How to correctly use PHP Json_encode function for Chinese conversion _php tutorial

Source: Internet
Author: User
What we're going to bring to you today is a workaround for the PHP json_encode function when it comes to handling Chinese conversions. JSON is a good data structure that is now widely used in network data transmission,

Json_encode and Json_decode

The specific use of the two functions on the internet has a lot of relevant articles, this article mainly introduces the Json_encode when the Chinese can not be converted to the solution, this paper assumes that the code used in the file is gb2312;

First, write the array you want.

  1. < ? Php
  2. $ JSON = Array (
  3. 0 = >
  4. Array (
  5. ' id ' = > ' A ',
  6. ' name ' = > ' ping-pong ',
  7. ),
  8. 1 = >
  9. Array (
  10. ' id ' = > ' + ',
  11. ' name ' = > ' Basketball ',
  12. )
  13. )
  14. ?>

If you use the PHP json_encode function directly

 
  
  
  1. < ? Php
  2. echo Json_encode ($json);
  3. ?>

The result is:

 
  
  
  1. < ? Php
  2. [{"id": "+", "name": null}
    , {"id": "+", "name": null}]
  3. ?>

You can see that Chinese characters are not escaped null because JSON simply escapes the encoding encoding, so the above statement should first convert the encoding

 
  
  
  1. < ? Php
  2. foreach ($ajax as $key=>$val)
  3. {

  4. UrlEncode ($val [' name ']);
  5. }
  6. echo Json_encode ($json);
  7. ?>

Client JS Code

 
 
  1. < script type="text/javascript">
  2. function Getsort (obj)
  3. {
  4. $.ajax (
  5. {
  6. Type: "GET",
  7. URL: " < ? = $this- > BASEURL ?> /index/getajax ",
  8. data: " C = "Obj.value,
  9. Success:function (JSON)
  10. {
  11. var JSON = Eval (JSON);
  12. var HTML = ' < select> ' ;
  13. $.each (JSON, function (k)
  14. {
  15. HTML = ' < option value= '
    json[k][' id '] '>'
    decodeURI (json[k][' name ']) '< /option>';
  16. });
  17. HTML = "</select>" ;
  18. $ (' #sort '). HTML (HTML);
  19. }
  20. }
  21. )
  22. }
  23. < /script >

Use the above code JS will error said code does not meet the standard

The reason is because JS in decodeURI only support UTF8 transcoding. So, the code for the PHP Json_encode function should be the following code

 
  
  
  1. < ? Php
  2. foreach ($ajax as $key=>$val)
  3. {

  4. UrlEncode (Iconv (' gb2312 '),
    ' Utf-8 ', $val [' name ']);
  5. }
  6. echo Json_encode ($json);
  7. ?>

The above is the solution to the problem in the actual operation using PHP Json_encode function.


http://www.bkjia.com/PHPjc/446101.html www.bkjia.com true http://www.bkjia.com/PHPjc/446101.html techarticle What we're going to bring to you today is a workaround for the PHP json_encode function when it comes to handling Chinese conversions. JSON is a good data structure that is now widely used in Web ...

  • 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.