Php outputs json and displays Chinese characters in json and examples

Source: Internet
Author: User
This article mainly introduces the php output of json and the detailed explanation of Chinese characters in json and the relevant information of the example. For more information, see php output of json and display of Chinese characters in json.

In php, we often need to output arrays as json. we only need to use the json_encode function to process the array, but sometimes the array contains Chinese characters, after processing using the json_encode function, Chinese characters are encoded into Unicode. how can I display Chinese characters in json? See the following.

First, we will introduce the php output json format:

The simplest method is to directly output an array as json, as shown below:

 'Ni "hao', 'P2' => 2, 'ch' => 'Hello, Coon! '); $ Json = json_encode ($ arr); echo $ json;?>

The output result is:

{"P1": "ni \" hao "," p2 ": 2," ch ":" \ u7801 \ u519c \ u4f60 \ u597d \ uff01 "}

Note:

Double quotation marks are automatically encoded as \ "in json. in JavaScript, single quotation marks, double quotation marks, and backslash are not allowed.
Chinese characters are encoded as Unicode

If it is a write interface, it is enough to output the data directly. After arriving at the client, convert the Chinese character of Unicode to Chinese characters. But if you want to output Chinese characters directly on the server side, we can do the following.

 'Nihao', 'P2' => 2, 'ch' => 'Hello, Coon! '); $ Json = json_encode ($ arr); echo decodeUnicode ($ json); function decodeUnicode ($ str) {return preg_replace_callback ('// \\\ u ([0-9a-f] {4})/I', create_function ('$ matches ', 'return mb_convert_encoding (pack ("H *", $ matches [1]), "UTF-8", "UCS-2BE"); '), $ str) ;}?>

Output:

{"P1": "ni \" hao "," p2 ": 2," ch ":"! "}

This operation is very simple. In fact, it is to match the output result and restore Unicode to Chinese characters.

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For details about php output json and display Chinese characters in json and related instance articles, refer to PHP Chinese website!

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.