[Daily] json_encode Differences between Chinese and quotation marks, json_encode quotation marks

Source: Internet
Author: User

[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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.