Solution to Chinese characters garbled by json_encode in php

Source: Internet
Author: User

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.


Example

The Code is as follows: Copy code

<? Php
$ 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

The Code is as follows: Copy code

<? Php
Foreach ($ jsonText as $ key => $ value)
{
$ JsonText [$ key] ['name'] = urlencode ($ value ['name']);
}
Echo json_encode ($ jsonText );
?>
Client Processing

<Script type = "text/javascript">
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 an error saying that the encoding does not conform to the standard.

The reason is that decodeURI in js only supports utf8 transcoding. Therefore, the code of the PHP json_encode function should be the following code:

The Code is as follows: Copy code

<? Php
Foreach ($ jsonText as $ key => $ value)
{
$ JsonText [$ key] ['name'] = urlencode (iconv ('gb2312', 'utf-8', $ value ['name']);
}
Echo json_encode ($ json );
?>

The output result is as follows:

{"Name": "u4e2du6587u5b57u7b26u4e32", "value": "test "}

It can be seen that UTF-8 characters are used, and Chinese garbled characters are also used in json_encode. 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:

The Code is as follows: Copy code

<? Php
$ 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 ));
?>

The output result is as follows:

The Code is as follows: Copy code
{"Name": "Chinese string", "value": "test "}

At this point, Chinese characters are successfully output. Use json_encode at will. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.