The php Tutorial comes with json_encode to process json data, so they do not support Chinese. Let's take a look at the example below.
Echo json_encode (array (123213, 'China '));
{"Platformid": "123213", "userid": "1023", "username": "u00b7u00f0u00b5u00b2u00c9u00b1u00b7u00f0u00ccu00fc "}
We will find that the English language can be correctly parsed, but the Chinese language is u00b7u00f0u00b5u00b2u00c9u00b1u00b7u00f0u00ccu00fc. This may be unicode code, but I did not test it but I guess it. Next we will look at a function to solve json Chinese garbled characters.
Private function to_utf8 ($ in)
{
If (is_array ($ in )){
Foreach ($ in as $ key => $ value)
{
$ Out [$ this-> to_utf8 ($ key)] = $ this-> to_utf8 ($ value );
}
}
Elseif (is_string ($ in ))
{
If (mb_detect_encoding () ($ in )! = "UTF-8 ")
Return utf8_encode ($ in );
Else
Return $ in;
}
Else
{
Return $ in;
}
Return $ out;
}
1. Output $ usr-> username directly, and set charset = utf-8 in the header of the page. Garbled characters
2. echo json_encode ($ usr) output username = null
3. Set the page header to charset = gbk. The output is correct.-> You can confirm that the original code is gbk.
Finally, through ie, chrome, and firefox tests, we can conclude that:
1. Ensure that the page character set is consistent with the database tutorial, and the output must be normal.
2. When json_encode is executed, ensure that the data encoding is UTF-8 and json_decode is normal.
3. If you want to perform json_encode on non-UTF-8 characters, convert them to UTF-8 first.
4. When json_decode is performed on non-UTF-8 characters, do not forget to convert them to the original encoding; otherwise, garbled characters will be output !!