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 );}