Solutions to Chinese problems when PHP processes JSON

Source: Internet
Author: User

JSON operations have recently been used in the project. The tests are normal. However, when the object is converted to JSON today, a Chinese attribute is changed to null.

OperatedCodeAs follows:

1 <? PHP
2 $ USR   =   New User ();
3 Echo Json_encode ( $ USR );
4 ?>

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:

1 Private   Function To_utf8 ( $ In )
2 {
3 If ( Is_array ( $ In )){
4 Foreach ( $ In   As   $ Key   =>   $ Value )
5 {
6 $ Out [ $ This -> To_utf8 ( $ Key )] =   $ This -> To_utf8 ( $ Value );
7 }
8 }
9 Elseif ( Is_string ( $ In ))
10 {
11 If (Mb_detect_encoding ( $ In ) ! =   " UTF-8 " )
12 Return   Utf8_encode ( $ In );
13 Else  
14 Return   $ In ;
15 }
16 Else
17 {
18 Return   $ In ;
19 }
20 Return   $ Out ;
21 }

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. Result: Garbled;
2. Echo json_encode ($ USR). Result: Output username = NULL;
3. Set the page header to charset = GBK. Result: The output is correct.-> you can confirm that the original code is GBK;

 

Finally, the test results are obtained through IE, chrome, and Firefox.Conclusion:

1. Ensure that the page character set is consistent with the database, and the output must be normal.
2. Ensure that the data encoding is UTF-8 and json_decode is normal during json_encode.
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 !!
after a day of trouble, the problem is finally solved. We hope that you can avoid detours and save time in the development process.

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.