This article lists 3 methods for implementing Json_encode () string to display the Chinese problem.
It's not necessary to do the interface, but it helps to save the log.
The official param and return must be affixed before the code is posted, link: http://php.net/manual/zh/function.json-encode.php
Parameters
-
value
-
To encode value , in addition to the resource type, can be any data type
This function can only accept UTF-8 encoded data.
-
options
-
A binary mask consisting of the following constants: JSON_HEX_QUOT , JSON_HEX_TAG JSON_HEX_AMP JSON_HEX_APOS JSON_NUMERIC_CHECK JSON_PRETTY_PRINT JSON_UNESCAPED_SLASHES JSON_FORCE_OBJECT JSON_UNESCAPED_UNICODE ,,,,,,,.
return value
Successful encoding returns a string that is represented as JSON or returned FALSE on failure.
<?php//Json_encode () Keep the Chinese method detailed $arr[' City ' = ' Beijing '; $arr [' name '] = ' weilong ';//Direct output//Res: {"City": "\u5317\u4eac", " Name ":" Weilong "}echo Json_encode ($arr)," \ n ";//1. Add parameter, PHP version >5.4.0//Res: {"City": "Beijing", "name": "Weilong"}echo json_encode ($arr, Json_unescaped_unicode), "\ n"; php > 5.4.0//2. Regular replace, after Json_encode, the code is replaced with a Chinese//Res: {"City": "Beijing", "name": "Weilong"}echo preg_replace ("#\\\u ([0-9a-f]{4}) #ie", " Iconv (' ucs-2be ', ' UTF-8 ', pack (' H4 ', ' \\1 ') ", Json_encode ($arr))," \ n ";//3. UrlDecode (), UrlEncode () function, not recommended//Res1:null, Res2: {"City": "Beijing", "name": "Weilong"}echo UrlDecode (Json_encode ( UrlEncode ($arr))), "\ n"; $arr [' city '] = UrlEncode ($arr [' City ']); The UrlEncode () parameter must be Stringecho UrlDecode (Json_encode ($arr)), "\ n", or//note the difference between the Json_decode () parameter. $arr [' city '] = ' Beijing '; $arr [' name '] = ' weilong '; $str = Json_encode ($arr); $str 2 = Json_decode ($STR); $str 3 = Json_decode ($str , true);p Rint_r ($str 2); object/* Res:stdclass Object ([City] = Beijing [name] = Weilong) */print_r ($str 3); array/* Res:array ([city] = Beijing [name] = weilong) */
PHP json_encode () Chinese encoding problem