The solution to the Chinese language problem when PHP processes json

  • 2020-03-31 21:38:07
  • OfStack

The operation code is as follows:
 
<?php 
$usr = new User(); 
echo json_encode($usr); 
?> 

Very simple code, no Chinese case all normal, output as follows:
{" PlatformID ":" 123213 ", "UserID" : "1023"}
There are two things that happen when you have Chinese.

In the first case, if the property with a Chinese value of the object itself is utf-8 encoded, the following output will be produced:


{" 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, but it's Chinese if we use firebug. (this has been bothering me for a long time)

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

{" PlatformID ":" 123213 ", "UserID" : "1023", "UserName" : null}
Oddly enough, after looking up the manual, it turns out that json_encode is only valid for utf-8, and all other encodings will be null.


The next step is to solve the problem of coding transformation.
See someone else's function on php.net:
 
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 down the conversion code found is not null. Under the joy of firebug open, found that is not my original Chinese characters... Start to struggle...
Was he supposed to convert to the original code? Go back and find the original code...
Test start:

1. The $usr - > UserName is output directly, and the page header is set to charset=utf-8
Echo json_encode($usr) outputs UserName=null
3. The page header is set as charset= GBK, the output is correct -> The original code can be determined as GBK
Finally, the conclusion is reached through the tests of IE,Chrome and Firefox:


1. Ensure that the page character set is consistent with the database, and the output must be normal.
2. When doing json_encode, ensure that the data encoding is utf-8 and json_decode is normal.
3. If you want to json_encode non-utf-8 characters, convert to utf-8 first.
4. When doing json_decode for non-utf-8 characters, do not forget to convert to the original encoding, otherwise it will output gargoyns!!
The problem that had been bothering me all day was finally solved.

Related articles: