Home  >  Article  >  Web Front-end  >  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
WBOYOriginal
2016-05-16 16:47:181732browse

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"}
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