Solutions to Chinese problems when php processes json

Source: Internet
Author: User

The operation code is as follows:
Copy codeThe Code is as follows: <? Php
$ Usr = new User ();
Echo json_encode ($ usr );
?>

The code is very simple. Everything works normally without Chinese characters. The output is as follows:
{"PlatformID": "123213", "UserID": "1023 "}
If you have a Chinese character, there may be two situations.

The first case is that the attribute of an object whose value is Chinese is UTF-8 encoded, the following output is displayed:

{"PlatformID": "123213", "UserID": "1023", "UserName ": "\ u00b7 \ u00f0 \ u00b5 \ u00b2 \ u00c9 \ u00b1 \ u00b7 \ u00f0 \ u00cc \ u00fc "}
The UserName is a non-human language, which is normal. If we use firebug, we can see it as Chinese. (I am struggling for a long time)

The second case is non-UTF-8 encoding, and the output will become null:

{"PlatformID": "123213", "UserID": "1023", "UserName": null}
It is strange that after checking the manual, we know that json_encode is only valid for UTF-8, and other encodings will become null.

The following describes how to solve the encoding conversion problem.
On php.net, we can see other functions as follows:Copy codeThe Code is as follows: 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;
}

So I found that the conversion encoding was not null. I opened it with firebug and found that it was not my original Chinese Character ......
Is it necessary to convert it into the original encoding? Go back and find the original encoding ......
Test start:

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, 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 !!
The problem that plagued the day was finally solved.

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.