In php, json_decode returns an array or an object instance.

Source: Internet
Author: User
Tags string format

1. json_decode ()

Json_decode
(PHP 5> = 5.2.0, PECL json> = 1.2.0)

Json_decode-encode strings in JSON format

Description
Mixed json_decode (string $ json [, bool $ assoc])
Accept a JSON string and convert it to a PHP variable

Parameters

Json
A string in json string format.

Assoc
If this parameter is set to TRUE, array instead of object is returned.


Return value
Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.

Example

Example #1 Example of json_decode ()

The code is as follows: Copy code

<? Php
$ Json = '{"a": 1, "B": 2, "c": 3, "d": 4, "e": 5 }';
Var_dump (json_decode ($ json ));
Var_dump (json_decode ($ json, true ));
?>

The above example will output:

Object (stdClass) #1 (5 ){
["A"] => int (1)
["B"] => int (2)
["C"] => int (3)
["D"] => int (4)
["E"] => int (5)
}

Array (5 ){
["A"] => int (1)
["B"] => int (2)
["C"] => int (3)
["D"] => int (4)
["E"] => int (5)
}


$ Data = '[{"Name": "a1", "Number": "123", "Contno": "000", "QQNo ":""}, {"Name": "a1", "Number": "123", "Contno": "000", "QQNo": "" },{ "Name ": "a1", "Number": "123", "Contno": "000", "QQNo": ""}] ';
Echo json_decode ($ data );

Result:

Array ([0] => stdClass Object ([Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] =>) [1] => stdClass Object ([Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] =>) [2] => stdClass Object ([Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ))

It can be seen that the object compiled by json_decode () is an object. Now, output json_decode ($ data, true) and try again.

The code is as follows: Copy code

Echo json_decode ($ data, true );

Result:

Array ([0] => Array ([Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] =>) [1] => Array ([Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] =>) [2] => Array ([Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ))

We can see an associated array output by json_decode ($ data, true). From this we can see that json_decode ($ data) outputs an object, while json_decode ("$ arr", true) is to force generate PHP associated array.


Assume that the JSON data we obtain is as follows: (you can use curl, fsockopen, and so on)

The code is as follows: Copy code

{
"From": "zh ",
"To": "en ",
"Trans_result ":[
  {
"Src": "u4f60u597d ",
"Dst": "Hello"
  }
 ]
}

I. json_decode:

Json_decode ($ data, true); use the json_decode function to return array:

The code is as follows: Copy code

Array
(
[From] => zh
[To] => en
[Trans_result] => Array
        (
[0] => Array
                (
[Src] => Hello
[Dst] => Hello
                )

        )

)

We can use the following methods in PHP to obtain the desired value:

The code is as follows: Copy code

<? Php
$ Data = <STR
{
"From": "zh ",
"To": "en ",
"Trans_result ":[
  {
"Src": "u4f60u597d ",
"Dst": "Hello"
  }
 ]
}
STR;
$ Jsondata = json_decode ($ data, true );
Header ("Content-Type: text/html; charset = UTF-8 ");
Print_r ($ jsondata); www.111cn.net
Echo "<br/>". $ jsondata ['to']; // en
Echo "<br/>". $ jsondata ['trans _ result'] [0] ['dst ']; // Hello
?>

II. json_decode:

Json_decode ($ data );

Use the json_decode function to return the object:

The code is as follows: Copy code

StdClass Object
(
[From] => zh
[To] => en
[Trans_result] => Array
        (
[0] => stdClass Object
                (
[Src] => Hello
[Dst] => Hello
                )

        )

)

We can use the following methods in PHP to obtain the desired value:

The code is as follows: Copy code

<? Php
$ Data = <STR
{
"From": "zh ",
"To": "en ",
"Trans_result ":[
  {
"Src": "u4f60u597d ",
"Dst": "Hello"
  }
 ]
}

STR;
$ Jsondata = json_decode ($ data );
Header ("Content-Type: text/html; charset = UTF-8 ");
Print_r ($ jsondata );
Echo "<br/>". $ jsondata-> from; // zh
Echo "<br/>". $ jsondata-> trans_result [0]-> src; // Hello
?>

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.