Resolve PHP function Json_encode after conversion Chinese is encoded as Unicode

Source: Internet
Author: User
Tags urlencode

We all know that using the function Json_encode () can quickly and easily convert an array into JSON-encoded, but if the array value exists in Chinese, Json_encode converts the Chinese to Unicode encoding, for example:

<? PHP $array Array (' China ', ' America ', ' Gao Shuai ', ' Bai Fu Mei ', ' Dick Silk '); echo json_encode ($array);

The results are printed as follows:

["\u4e2d\u56fd", "\U7F8E\U56FD", "\u9ad8\u5bcc\u5e05", "\u767d\u5bcc\u7f8e", "\u5c4c\u4e1d"]

This format is also our most common, and can be parsed in JavaScript. But in some cases I do not need to convert Chinese to Unicode encoding, if it is handled. The solution is to first UrlEncode () the Chinese. Then turn to JSON. The JSON transformation can be resolved by UrlDecode () :

$data Array (); foreach ($arrayas$key=$value) {    $dataUrlEncode ($value);} Echo UrlDecode (Json_encode ($data));

Printing results are:

["China","United States","high-rich handsome","White rich beauty","Dick Silk"]

In this way, our problem is solved, the common weather-forecasting interface return value is this. It is important to note that Json_encode () only supports UTF-8 encoding . So if it's another code, you need to convert it. such as: Iconv (), of course, a lot of methods, in the PHP5.4 version of Json_encode () has supported the non-conversion encoding, for example: Json_encode ($str, Json_unescaped_unicode)

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.