PHP outputs a Chinese JSON string

Source: Internet
Author: User

PHP and JavaScript are actually very convenient. PHP native also provides support for JSON format. Mainly includes two functions: JSON encoding and decoding:

Json_endoce: http://cn.php.net/json_encode

Json_dedoce: http://cn.php.net/json_decodejson_encode-encode the variable in JSON format and return the value, for example:

$ Arr = array ('A' => 1, 'B' => 2, 'c' => 3, 'D' => 4, 'E' => 5 );

Echo json_encode ($ arr );

?>

Output the code after execution:

{"A": 1, "B": 2, "c": 3, "d": 4, "e": 5}

For example, if the data source to be encoded (usually an array) contains Chinese characters, after json_encode processing, unicode encoding is output.

$ Arr = array ('A' => 'mango xiaozhan ');

Echo json_encode ($ arr );

?>

Output the code after execution:

{"A": "\ u8292 \ u679C \ u5C0F \ u7AD9 "}

Unicode processing has been performed at the underlying layer of PHP. If it is not intuitive enough, you can use the urlencode and urldecode methods to bypass the process of transcoding to unicode:

$ Arr = array ('A' => urlencode ('mango xiaozhan '));

Echo urldecode (json_encode ($ arr); output after the above code is executed:

{"A": "Mango xiaozhan "}

Related Article

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.