PHP tutorial comes with Json_encode to process JSON data, then their support for Chinese is not good, let's look at an example
echo Json_encode (Array (123213, ' China '));
{"PlatformID": "123213", "userid": "1023", "username": "U00B7U00F0U00B5U00B2U00C9U00B1U00B7U00F0U00CCU00FC"}
We will find that the English can be correctly parsed, but Chinese will come out U00B7U00F0U00B5U00B2U00C9U00B1U00B7U00F0U00CCU00FC Oh, this may be Unicode encoding, but I did not test it just to guess. Let's look at a function to solve the JSON Chinese garbled
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. 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 tutorial, 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!!
http://www.bkjia.com/PHPjc/632302.html www.bkjia.com true http://www.bkjia.com/PHPjc/632302.html techarticle PHP Tutorial comes with Json_encode to process JSON data, then their support for Chinese is not good, let's look at an example of Echo Json_encode (Array (123213, ' China ')); {Platformid:123213,u ...