php json decode garbled solution: first, before JSON processing, convert the characters into UTF8 form; then use "json_encode" in the background PHP page to convert the array array in PHP into a JSON string. Can.
PHP json_encode Chinese garbled solution
I believe many people use Ajax to interact with the background php page I have all encountered the problem of garbled Chinese characters. As a lightweight data exchange format, JSON is very popular. However, using PHP as the backend interaction is prone to the problem of Chinese garbled characters.
JSON is the same as js. Client characters are processed in the form of UTF8. That is to say, when using JSON as the data format for submission and reception, the characters are processed by UTF8 encoding. When our page When encoding and database encoding do not use UTF8, it is extremely easy for Chinese garbled characters to appear. The solution is naturally to use UTF8 when processing JSON data with js or PHP.
PHP5.2 or above uses json_encode as a built-in function, which brings great convenience to website makers. However, we must note that json_encode only supports UTF8 encoding. characters, otherwise, Chinese garbled characters or null values will appear.
The solution is divided into the following two steps.
Step1
Ensure that characters are encoded in UTF8 when processing JSON. Specifically, we can change both the database encoding and page encoding to UTF8. Of course, if you like to use gbk encoding, you can convert the characters to UTF8 before JSON processing. There are the following methods in PHP:
Step2
Background PHP page (page Encoded to UTF-8 or the characters have been converted to UTF-8) Use json_encode to convert the array array in PHP into a JSON string. For example:
'中文字符串','value'=>'test'); echo json_encode($testJSON); ?>
View the output result as:
{“name”:”\u4e2d\u6587\u5b57\u7b26\u4e32″,”value”:”test”}
It can be seen that even if UTF8-encoded characters are used, Chinese garbled characters appear when using json_encode. The solution is to use the urlencode() function to process the characters before using json_encode, then json_encode them, and use the urldecode() function to convert them back when outputting the results. The details are as follows:
'中文字符串','value'=>'test'); //echo json_encode($testJSON); foreach ( $testJSON as $key => $value ) { $testJSON[$key] = urlencode ( $value ); } echo urldecode ( json_encode ( $testJSON ) ); ?>
Check the output result as:
{“name”:”中文字符串”,”value”:”test”}
At this point, Chinese characters are successfully output. Feel free to use json_encode. In this way, the JSON string output in the PHP background will not appear Chinese garbled when eval is received by Ajax in the front-end javascript, because js also processes JSON format data in the form of UTF8, which is similar to PHP, so it receives the PHP page The JSON string does not cause problems.
Recommended: "PHP Tutorial"
The above is the detailed content of What to do if php json decode Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!