The general solution is to ensure that the characters are UTF-8 encoded during JSON processing. For details, we can change the database encoding and page encoding to UTF8. Of course, if you like to use gbk encoding, you can
The general solution is to ensure that the characters are UTF-8 encoded during JSON processing. For details, we can change the database encoding and page encoding to UTF8. Of course, if you like to use gbk encoding, you can convert the character into UTF8 format before JSON processing.
I. json_encode ()
This is a common function for JSON encoding of variables. However, when the text format is not UTF-8, Chinese transcoding may encounter some problems, such as when the text is gb2312, for example:
-
- $ JsonText = array (
- 0 => array (
- 'Id' => '1 ',
- 'Name' => 'text 1'
- ),
- 1 => array (
- 'Id' => '2 ',
- 'Name' => 'text 2'
- )
- );
- Echo json_encode ($ jsonText );
- // [{"Id": "1", "name": "" },{ "id": "2", "name": ""}]
- // We can see that the Chinese characters are not escaped as empty "", because json only escapes the encoding (similar to: % B0 % AE), so the preceding statement should first convert the encoding
- ?>
Solution:
-
- Foreach ($ jsonText as $ key => $ value)
- {
- $ JsonText [$ key] ['name'] = urlencode ($ value ['name']);
- }
- Echo json_encode ($ jsonText );
- ?>
Client processing:
-
- Function encodeTest (obj)
- {
- $. Ajax ({
- Type: "GET ",
- Url: "<? = $ This-> baseUrl?> /Index/getajax ",
- Data: "c =" obj. value,
- Success: function (json)
- {
- Var json = eval (json );
- Var testValue = '';
- $. Each (json, function (k ){
- TestValue + = decodeURI (json [k] ['name']);
- });
-
- Alert (testValue );
- }
- })
-
- }
- Script
The above code js reports that the encoding does not conform to the standard because decodeURI in js only supports utf8 transcoding. Therefore, the code of the PHP json_encode function should be the following code:
-
- Foreach ($ jsonText as $ key => $ value)
- {
- $ JsonText [$ key] ['name'] = urlencode (iconv ('gb2312', 'utf-8', $ value ['name']);
- }
- Echo json_encode ($ json );
- ?>
The output result is {"name": "u4e2du6587u5b57u7b26u4e32", "value": "test"}. it can be seen that UTF-8 characters are used, and json_encode also contains Chinese characters. The solution is to use the urlencode () function to process the characters before using json_encode, and then use json_encode to return the urldecode () function when outputting the results. The details are as follows:
-
- $ TestJSON = array ('name' => 'Chinese string', 'value' => 'test ');
- // Echo json_encode ($ testJSON );
- Foreach ($ testJSON as $ key => $ value ){
- $ TestJSON [$ key] = urlencode ($ value );
- }
- Echo urldecode (json_encode ($ testJSON ));
- ?>
Check that the output result is {"name": "Chinese string", "value": "test"}. at this point, the Chinese character is successfully output. use json_encode as needed, in this way, the JSON string output in the PHP background will not contain Chinese garbled characters after Ajax is received in the front-end javascript, because js is also processing JSON format data in the form of UTF8, similar to PHP, the JSON string that receives the PHP page will not be faulty.