[Daily] json_encode Differences between Chinese and quotation marks, json_encode quotation marks
Json_encode ()
1. By default, all characters other than ASCII characters that can be displayed are escaped as Unicode.
If the characters are escaped as Unicode, no garbled characters will occur regardless of whether the file encoding is consistent. Therefore, converting Chinese to Unicode encoding is advantageous.
2. The processing of single quotes and double quotes is different, and is affected by the quotation marks of strings wrapped in array key values.
3. json_encode (array (), JSON_FORCE_OBJECT); Outputs An empty object.
Json_encode (new stdClass (); another method
JSON_UNESCAPED_SLASHES do not escape/. For example, many URLs are escaped.
JSON_UNESCAPED_UNICODE Chinese characters cannot be encoded as Unicode characters
<? Phpvar_dump (json_encode (array (); var_dump (json_encode (array (), JSON_FORCE_OBJECT); var_dump (json_encode (new stdClass ())); var_dump (json_encode (array ('name' => 'taos Hans', 'point' => 100, 'test' => '/aaa \ "\' \ AA '), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); var_dump (json_encode (array ("name" => "Tao shihan", "point" => 100, "test" => "/aaa \" \ ''\ aa"), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);/** output string (2) "[]" string (2) "{}" string (2) "{}" string (64) "{" name ":" Tao shihan "," point ": 100," test ": "/aaa \" '\ aa \ r \ n "}" string (65) "{" name ":" Tao shihan "," point ": 100, "test": "/aaa \" \ ''\ aa \ r \ n "}"
Important:
1. The backslash with the escape effect will not change, and the backslash without the escape effect will be automatically escaped.
2. The single quotation marks of the Package content outside the array do not affect the output. After the output, all strings are enclosed in double quotation marks, but the first point is affected.
4. The json_decode failure occurs when an error occurs:
Var_dump (json_last_error (); // print the error message
Var_dump ($ B );
0 JSON_ERROR_NONE
1 JSON_ERROR_DEPTH
2 JSON_ERROR_STATE_MISMATCH
3 JSON_ERROR_CTRL_CHAR
4 JSON_ERROR_SYNTAX
5 JSON_ERROR_UTF8
6 JSON_ERROR_RECURSION
7 JSON_ERROR_INF_OR_NAN
8 JSON_ERROR_UNSUPPORTED_TYPE
Handle this problem when the encoding is faulty.
$ A = iconv ("gbk", "UTF-8 // IGNORE", $ );
$ B = json_decode ($ a, true );