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:
<? 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)
} Http://www.111cn.net/phper/php-cy/57800.htm
$ 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:
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:
{
"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:
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:
<? 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:
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:
<? 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
?>
For more details, see: http://www.111cn.net/phper/php-cy/57800.htm