When using json data, many of my friends may use the JSON_DECODEJSON_ENCODE function of php to process Chinese content, which may cause NULL or garbled characters. what can I introduce to you below?
Many friends may encounter NULL or garbled characters when using the JSON_DECODE/JSON_ENCODE function of php to process Chinese content when using json data, next I will introduce to you why this problem occurs, for example:
-
- $ Json = '{"a": 1, "B": 2, "c": 3, "d": 4, "e": 5 }';
- Var_dump (json_decode ($ json ));
- Var_dump (json_decode ($ json, true ));
- ?>
Output result
Object (stdClass) #1 (5) {["a"] => int (1) ["B"] => int (2) ["c"] => int (3) ["d"] => int (4) ["e"] => int (5)} array (5) {["a"] => int (1) ["B"] => int (2) ["c"] => int (3) ["d"] => int (4) ["e"] => int (5 )}
If it is completely correct and there is no problem, we test the Chinese language and the code is as follows:
-
- $ Json = '{"a": ""}';
- Var_dump (json_decode ($ json ));
- ?>
Result: {"text": null, "status": 1}. it was obtained from the php manual that json_encode and json_decode only support UTF-8 characters, the GBK character must be converted to json, so that we can easily convert it.
The code for converting an encoding is as follows:
- /*
- The string GBK is transcoded to a UTF-8, and the number is converted to a number.
- */
- Function ct2 ($ s ){
- If (is_numeric ($ s )){
- Return intval ($ s );
- } Else {
- Return iconv ("GBK", "UTF-8", $ s );
- }
- }
- /*
- Batch processing gbk-> UTF-8
- */
- Function icon_to_utf8 ($ s ){
- If (is_array ($ s )){
- Foreach ($ s as $ key => $ val ){
- $ S [$ key] = icon_to_utf8 ($ val );
- }
- } Else {
- $ S = ct2 ($ s );
- }
- Return $ s;
- }
- Echo json_encode (icon_to_utf8 ("Xiamen "));
In this case, there are still some problems. later I found a method to process all the content in the array using urlencode () before json_encode, and then convert it to a json string using json_encode, finally, urldecode () is used to convert the encoded Chinese characters and write a function:
- /*************************************** ***********************
- *
- * 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 whether it is also applied to the key
- * @ Access public
- *
- **************************************** *********************/
- Function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false)
- {
- 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]);
- }
- }
- }
- }
- /*************************************** ***********************
- *
- * 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 );
- Return urldecode ($ json );
- }