Home > Web Front-end > JS Tutorial > Teach you how to use PHP to output Chinese JSON string_json

Teach you how to use PHP to output Chinese JSON string_json

WBOY
Release: 2016-05-16 16:47:18
Original
1744 people have browsed it

Copy code The code is as follows:

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

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

Copy code The code is as follows:

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

Output after executing the above code:

Copy code The code is as follows:

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

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

Copy code The code is as follows:

$arr = array ( 'a'=>'Script Home');
echo json_encode($arr);
?>

Output after executing the above code:

Copy code The code is as follows:

{"a":"u811au672cu4e4bu5bb6"}

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

Copy code The code is as follows:

$arr = array ('a'=>urlencode ('Script Home'));
echo urldecode(json_encode($arr));

Output after executing the above code:

Copy code The code is as follows:

{"a":"Script Home"}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template