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!