How to solve the Chinese problem when PHP handles JSON _php tips

Source: Internet
Author: User
The code for the operation is as follows:
Copy Code code as follows:

<?php
$USR = new User ();
echo Json_encode ($USR);
?>

Very simple code, no Chinese situation all normal, the output is as follows:
{"PlatformID": "123213", "UserID": "1023"}
Once you have Chinese, there are two situations.

The first case is that an attribute with a value of the object itself is utf-8 encoded, and the following output is available:


{"PlatformID": "123213", "UserID": "1023", "UserName": "\u00b7\u00f0\u00b5\u00b2\u00c9\u00b1\u00b7\u00f0\u00cc\ U00FC "}
One of the username is not human language, this is normal, if we use firebug look is Chinese. (This tangled me half a day)

The second case is UTF-8 encoded and the output becomes null:

{"PlatformID": "123213", "UserID": "1023", "UserName": null}
Curiously, after checking the manual, we know that Json_encode is only valid for Utf-8, and that all other encodings will become null.


The following is the beginning of the problem of coding conversion.
The function of seeing someone on the php.net is as follows:
Copy Code code 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 take it down. The conversion encoding discovery is no longer null. Rejoice to open with Firebug, discover is not my original Chinese character .... Start to Tangle ...
Do you want him to convert to the original code? Go back and look for the original code ...
Test start:

1. $usr->username Direct output, page header set Charset=utf-8. garbled
2.echo json_encode ($usr) Output username=null
3. The page header is set to CHARSET=GBK, the output is correct-> can determine the original code is GBK
Finally through the Ie,chrome,firefox test to conclude:


1. Ensure the page character set is consistent with the database, the output must be normal.
2. Do json_encode to ensure that the data encoding is Utf-8,json_decode normal.
3. If you want to do json_encode to utf-8 characters, first convert to Utf-8.
4. For utf-8 characters do Json_decode, you must not forget to convert to the original code, otherwise it will output garbled!!
The problems that have plagued the day have finally been settled.

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.