Two times Json_encode () and two times Json_decode ()

Source: Internet
Author: User

I didn't notice these two functions before, and someone in the group asked yesterday. Discover the following things.

Header ("content-type:text/html;charset=utf-8"= Array ('a '=1,'b'== 2=  = == json_decode ($arr _1); Var_dump ($obj); Echo $obj->a;

Results:

String ' {"A": 1, "B": 2} ' (length=13) string ' "{\" a\ ": 1,\" B\ ": 2}" ' (length=19) string ' "{\" a\ ": 1,\" B\ ": 2}" ' (length=19) String ' {' A ': 1, ' B ': 2} ' (length=13) object (StdClass) [1] public  ' a ' = = int 1 public  ' b ' = = int 21

Using Json_encode () two or n times will escape the previously-turned JSON string.

Look at the API for Json_encode ()

String Json_encode ( mixed $value [, int $options = 0])

This says that the first argument is a mixed type, except that the rest of the resource can. The following parameters. Don't want to deviate from the theme.

Since the first call to Json_encode () is an array, the second time the Json_encode () method is called, the argument is a string.

With Json_decode ();

Mixed json_decode ( string $json [, bool $assoc = False [, int $depth = [ +], in T $options = 0]])

The API says that only JSON strings can be placed.

But when we json_decode the second time, the arguments passed in are actually an array. Returned to us with an object.

Two times Json_encode () and two times Json_decode ()

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.