How to output Chinese JSON string using PHP

  • 2020-03-30 03:04:04
  • OfStack


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

Json_encode - a JSON form that encodes the variable JSON and returns the value, for example:


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

Output after the above code execution:


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

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


<?php
$arr = array ('a'=>' The home of the script ');
echo json_encode($arr);
?>

Output after the above code execution:


{"a":"u811au672cu4e4bu5bb6"}

PHP has done unicode processing at the bottom, if it is not intuitive enough, you can use urlencode and urldecode methods to bypass this transcoding to unicode process:


$arr = array ('a'=>urlencode(' The home of the script '));
echo urldecode(json_encode($arr));

Output after the above code execution:


{"a":" The home of the script "}


Related articles: