' Shia ', ' age ' =>20); $jsonencode = Json_encode ($arr); Echo $jsonencode; >
The results of the program run as follows:
Iconv (' gb2312′, ' utf-8′, ' here is the Chinese title '), ' body ' = ' abcd ... '); Echo Json_encode ($array);? >
The result of this program is:
{"title": "\u8fd9\u91cc\u662f\u4e2d\u6587\u6807\u9898″," "Body": "ABCD ..."}
All the Chinese in the array have disappeared after the Json_encode, or appear \u2353. The workaround is to use the UrlEncode () function to handle the following, before Json_encode, all the contents of all arrays are processed with UrlEncode (), and then converted to a JSON string with Json_encode (), and then UrlDecode () Turn the encoded Chinese back.
(' Possible deep recursion attack ')} foreach ($array as $key = + $value) {if (Is_array ($value)) {arrayrecursive ($array [$key], $function, $apply _to_keys_ also);} else {$array [$key] = $function ($value);} if ($apply _to_keys_also && is_string ($key)) {$new _key = $function ($key), if ($new _key! = $key) {$array [$new _key] = $array [$key];unset ($array [$key]);}} $recursive _counter–;} /**************************************************************** convert an array to a JSON string (Chinese language compatible) * @param array $array the arrays to be converted * @ Return string converted to JSON string * @access public**************************************************************/function JSON ($array) {arrayrecursive ($array, ' UrlEncode ', true), $json = Json_encode ($array); return UrlDecode ($json);} $array = Array (' Name ' = ' Shia ', ' age ' =>20); Echo JSON ($array); >
This success, the results of the operation are as follows:
{"Name": "Shia", "Age": "20″}"