Chinese encoding function after json_encode [reprinted]

Source: Internet
Author: User
Symptom: It is well known that json_encode can be used to easily and quickly encode objects in json format. However, if the attributes of an object contain Chinese characters, the problem will arise. Json_encode converts Chinese to unicode encoding. For example, after the encoding is processed by json_encode, it is changed to u80e5. In the final json, the Chinese part is replaced with unicode encoding. We

Symptom: It is well known that json_encode can be used to easily and quickly encode objects in json format. However, if the attributes of an object contain Chinese characters, the problem will arise. Json_encode converts Chinese to unicode encoding. For example, after the encoding is processed by json_encode, it becomes \ u80e5, and the Chinese part of the final json is replaced with unicode encoding. We

Symptom: It is well known that json_encode can be used to easily and quickly encode objects in json format. However, if the attributes of an object contain Chinese characters, the problem will arise. Json_encode converts Chinese to unicode encoding. For example, after 'encoding' is processed by json_encode, it is changed to '\ u80e5'. In the final json, the Chinese part is replaced with unicode encoding. What we need to solve is to convert the object to json and ensure that the Chinese character inside the object still appears in json as normal Chinese. Now it seems that json_encode alone cannot achieve the goal.
1. If your php version is 5.4, simply add the JSON_UNESCAPED_UNICODE parameter.
$ Test = json_encode ("try", JSON_UNESCAPED_UNICODE );
2. urlencode, json_encode, and urldecode are used to ensure that Chinese characters are not converted to unicode. The related functions are as follows:

@ Share www. ziqing. cc
<无>
/*************************************** * *********************** Use a specific function to process all elements in the array * @ param string & $ array the string to be processed * @ param string $ function the function to be executed * @ return boolean $ apply_to_keys_also is also applied to the key * @ access public * @ share www. ziqing. cc *************************************** * *******************/function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false) {static $ recursive_cou Nter = 0; if (++ $ recursive_counter> 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 * @ share www. ziqing. cc *************************************** * ********************/function JSON ($ array) {arrayRecursive ($ array, 'urlencode', true); $ json = json_encode ($ array); return urldecode ($ json );}

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.