Home  >  Article  >  php教程  >  Teach you how to use PHP to output Chinese JSON strings

Teach you how to use PHP to output Chinese JSON strings

高洛峰
高洛峰Original
2016-12-27 16:20:581015browse

json_endoce: http://cn.php.net/json_encode
json_dedoce: http://cn.php.net/json_decode

json_encode — JSON encodes the variable and returns the JSON form of the value, for example:

1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>

The output after the above code is executed:

{"a":1,"b":2,"c":3,"d":4,"e":5}

If the data source to be encoded (generally is an array), the value contains Chinese, and the output after json_encode processing is unicode encoding.

'脚本之家');
echo json_encode($arr);
?>

The output after the above code is executed:

{"a":"\u811a\u672c\u4e4b\u5bb6"}

The bottom layer of PHP has been processed by unicode. If you think it is not intuitive enough, you can use the urlencode and urldecode methods to bypass the process of transcoding to unicode:

$arr = array ('a'=>urlencode('PHP中文网'));
echo urldecode(json_encode($arr));

After the above code is executed, the output is:

{"a":"PHP中文网"}

For more related articles that teach you how to use PHP to output Chinese JSON strings, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn