Recently encountered a strange thing, the first two days to write a good code, today again run is garbled. Checked, not the general meaning of garbled, but the return of Unicode encoded characters.
such as Kanji: Login failed, after Json_encode returned to:"\U767B\U5F55\U5931\U8D25"
Really angry Zajia, find a pass, found that this is a common problem, the final solution is as follows:
Normal call Json_encode ($arr), between Echo, this JSON data is handled by the following function:
public static function JSON ($STR) {
$json = Json_encode ($STR);
Return Preg_replace ("#\\\u ([0-9a-f]+) #ie", "Iconv (' UCS-2 ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $json);
}
This will no longer garbled. For a high version of PHP, you can add a parameter directly, but the lower version is not valid, so you can only do so. The complete code is as follows:
<?phpclass util{public static function JSON ($STR) {$json = Json_encode ($STR); return Preg_replace ("#\\\u ([0-9a-f]+) # ie "," iconv (' UCS-2 ', ' UTF-8 ', pack (' H4 ', ' \\1 ')) ", $json);}}? >
Invocation Example:
echo "Login Failed"; $arr = Array (); $arr [' code '] = 1; $arr [' message '] = "Login Failed"; echo $arr [' message ']; $arr [' data '] = ""; retu RN Util::json ($arr);
Reference: Http://stackoverflow.com/questions/6771938/any-way-to-return-php-json-encode-with-encode-utf-8-and-not-unicode
PHP Json_encode always returns the Unicode character "\u ..." Problem resolution