It seems that only UTF-8/ASCII encoding is supported when arrays/objects are serialized as JSON strings with json_decode. Some of our sites use GBK/GB2312 encoding. In this case, problems may occur when json_encode/json_decode is used directly.
| The code is as follows: |
Copy code |
|
<? Php
$ Json = '{"a": ""}';
Var_dump (json_decode ($ json ));
?>
Result
{"Text": null, "status": 1} |
For example, if the conversion of characters containing Chinese characters becomes null, but sometimes we have to use gb encoding and json_decode () conversion? What should I do? I wrote a small background for adding music without using a database last night. That is, add music using php and then generate an xml menu. You can save the data in one way without using a database. The data saved as text can be used out directly. I think it is used to convert the array json_decode () into json format, you can use json_encode to convert it back to an array. (it may be affected by json recently. It seems that there are better methods to serialize the array. For example, use: serialize () and unserialize. Since json_decode () cannot be converted to Chinese in gb encoding, we can convert Chinese into English encoding first. You can use this urlencode () to convert the code and then perform the json_decode () conversion. You can use urldecode () to convert it to Chinese.
| The code is as follows: |
Copy code |
|
/*
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 ")); |