The json_encode function is encoded as null in Chinese.

Source: Internet
Author: User
The json_encode function is encoded as null in Chinese.
 'Age', 'age' => 20); $ jsonencode = json_encode ($ arr); echo $ jsonencode;?>
The program running result is as follows:

 Iconv ('gb2312', 'utf-8', 'Here is the Chinese title'), 'body' => 'abcd... '); Echo json_encode ($ array);?>
The running result of this program is:

{"Title": "\ u8fd9 \ u91cc \ u662f \ u4e2d \ u6587 \ u6807 \ u9898", "body": "abcd ..."}

All Chinese characters in the group are missing after json_encode or \ u2353 is displayed. The solution is to use the urlencode () function to process the following. before json_encode, urlencode () is used to process all the content in the array, and json_encode () is used to convert the content into a json string, finally, use urldecode () to convert the encoded Chinese characters back.

 1000) {die ('possible deep recursion attack');} foreach ($ array as $ key => $ value) {if (is_array ($ value )) {arrayRecursive ($ array [$ key], $ function, $ apply_to_keys_also);} else {$ array [$ key] = $ function ($ value );} if ($ apply_to_keys_also & is_string ($ key) {$ new_key = $ function ($ key); if ($ new_key! = $ Key) {$ array [$ new_key] = $ array [$ key]; unset ($ array [$ key]) ;}}$ recursive_counter -;} /*************************************** * ************************ convert an array to a JSON string (compatible with Chinese characters) * @ param array $ array the array to be converted * @ return string the converted json string * @ access public ***************** **************************************** * ***/function JSON ($ array) {arrayRecursive ($ array, 'urlencode', true); $ json = json_encode ($ array); re Turn urldecode ($ json);} $ array = array ('name' => 'chia', 'age' => 20); echo JSON ($ array);?>
The operation is successful. The result is as follows:

{"Name": "Sia", "Age": "20 ″}

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.