How to solve Chinese problems in PHP when working with JSON _php tutorial

Source: Internet
Author: User
The code for the operation is as follows:
Copy CodeThe code is as follows:
$USR = new User ();
echo Json_encode ($USR);
?>

Very simple code, no Chinese situation everything is normal, the output is as follows:
{"PlatformID": "123213", "UserID": "1023"}
There are two cases when there is a Chinese language.

The first case is that a property of the object itself that has a value of Chinese is utf-8 encoded, then the output is as follows:


{"PlatformID": "123213", "UserID": "1023", "UserName": "\u00b7\u00f0\u00b5\u00b2\u00c9\u00b1\u00b7\u00f0\u00cc\ U00FC "}
The username is non-human language, this is normal, if we use Firebug to see is Chinese. (This tangled me half a day)

The second case is a non-utf-8 encoding, and the output becomes null:

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


Here's how to solve the problem of encoding conversion.
The functions of seeing others on Php.net are 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 take it down. Conversion encoding discovery is not NULL. Delighted to open with Firebug, found not my original Chinese characters .... Start to Tangle ...
Do you want him to be converted into the original code? Go back and look for the original code ...
Test start:

1. Put $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 be determined that the original code is GBK
Finally, the conclusion is obtained by Ie,chrome,firefox test:


1. Ensure that the page character set is consistent with the database, the output must be normal.
2. Ensure that data encoding is utf-8,json_decode normal when doing json_encode.
3. If you want to do json_encode for non-utf-8 characters, first convert to Utf-8.
4. For non-utf-8 characters do json_decode, do not forget to convert to the original code, otherwise it will output garbled!!
The problem that bothered the day was finally settled.

http://www.bkjia.com/PHPjc/323182.html www.bkjia.com true http://www.bkjia.com/PHPjc/323182.html techarticle the code for the operation is as follows: Copy the code as follows: PHP $usr = new User (); Echo Json_encode ($USR); Very simple code, no Chinese condition everything is OK, output is as follows: {"PlatformID" ...

  • Related Article

    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.