How to correctly use the PHPjson_encode function to convert Chinese characters _ PHP Tutorial

Source: Internet
Author: User
Use the PHPjson_encode function to convert Chinese characters. What we will bring to you today is the solution to the problem that occurs when the PHPjson_encode function is processing the Chinese conversion. Json is a good data structure and is now widely used on the internet. what we will bring to you today is the solution to the problem that occurs when the PHP json_encode function handles Chinese conversion. Json is a good data structure and is now widely used in network data transmission,

Json_encode and json_decode

There are many related articles on the Internet about the specific usage of these two functions. This article mainly introduces the solution for converting Chinese characters when using json_encode. This article assumes that the file is encoded as gb2312;

Write out the required array first

  1. <? PHP
  2. $ Json = array (
  3. 0 =>
  4. Array (
  5. 'Id' =>'13 ',
  6. 'Name' =>'Table tennis ',
  7. ),
  8. 1 =>
  9. Array (
  10. 'Id' =>'17 ',
  11. 'Name' =>'Basketball ',
  12. )
  13. )
  14. ?>

If you directly use the PHP json_encode function

 
 
  1. < ?PHP
  2. echo json_encode($json);
  3. ?>

Result:

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

We can see that the Chinese characters are not escaped as null because json only escapes the encoding, so the preceding statement should first convert the encoding.

 
 
  1. < ?PHP
  2. foreach ($ajax as $key=>$val)
  3. {
  4. $ajax[$key]['name'] =
    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>

The above code js reports an error saying that the encoding does not conform to the standard.

The reason is that decodeURI in js only supports utf8 transcoding. Therefore, the code of the PHP json_encode function should be the following code:

 
 
  1. < ?PHP
  2. foreach ($ajax as $key=>$val)
  3. {
  4. $ajax[$key]['name'] =
    urlencode(iconv('gb2312',
    'utf-8',$val['name']));
  5. }
  6. echo json_encode($json);
  7. ?>

The above is how to solve the problem in actual operations using the PHP json_encode function.


The json_encode function solves the problem when processing the Chinese conversion. Json is a good data structure and is now widely used in the internet...

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.