1.json_decode()
json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_decode — Encode a JSON-formatted string
Description
mixed json_decode ( string $json [, bool $assoc ] )
Accepts a JSON formatted string and converts it into a PHP variable
Parameters
json
The string in json string format to be decoded.
assoc
When this parameter is TRUE, array will be returned instead of object.
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 |
|
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
代码如下 |
|
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
上例将输出:
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);
结果为:
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] => ) )
|
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);
The result is:
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] = > ) )
|
可以看出经过json_decode()编译出来的是对象,现在输出json_decode($data,true)试下
代码如下 |
|
echo json_decode($data,true);
代码如下 |
|
echo json_decode($data,true);
结果:
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] => ) )
|
结果:
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] => ) )
|
可以看出 json_decode($data,true)输出的一个关联数组,由此可知json_decode($data)输出的是对象,而json_decode("$arr",true)是把它强制生成PHP关联数组.
代码如下 |
|
{
"from":"zh",
"to":"en",
"trans_result":[
{
"src":"u4f60u597d",
"dst":"Hello"
}
]
}
|
假如我们获取的JSON数据如下:(可以使用curl、fsockopen等方式获取)
代码如下 |
|
{
"from":"zh",
"to":"en",
"trans_result":[
{
"src":"u4f60u597d",
"dst":"Hello"
}
]
}
|
一、json_decode返回array的方式:
json_decode($data,true);用json_decode函数返回array的方式得到:
代码如下 |
|
Array
(
[from] => zh
[to] => en
[trans_result] => Array
(
[0] => Array
(
[src] => 你好
[dst] => Hello
)
代码如下 |
|
Array
(
[from] => zh
[to] => en
[trans_result] => Array
(
[0] => Array
(
[src] => 你好
[dst] => Hello
)
)
)
|
)
)
|
代码如下 |
|
$data = <<
{
"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 " ".$jsondata['to']; //en
echo " ".$jsondata['trans_result'][0]['dst']; //Hello
?>
|
我们在PHP语言中可以用以下方法取得我们想要的值:
代码如下 |
|
$data = <<
{
"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 " ".$jsondata['to']; //en
echo " ".$jsondata['trans_result'][0]['dst']; //Hello
?>
|
二、json_decode返回object的方式:
json_decode($data);
用json_decode函数返回object的方式得到:
代码如下 |
|
stdClass Object
(
[from] => zh
[to] => en
[trans_result] => Array
(
[0] => stdClass Object
(
[src] => 你好
[dst] => Hello
)
代码如下 |
|
stdClass Object
(
[from] => zh
[to] => en
[trans_result] => Array
(
[0] => stdClass Object
(
[src] => 你好
[dst] => Hello
)
)
)
|
)
)
|
代码如下 |
|
$data = <<
{
"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 " ".$jsondata->from; //zh
echo " ".$jsondata->trans_result[0]->src; //你好
?>
|
我们在PHP语言中可以用以下方法取得我们想要的值:
代码如下 |
|
$data = <<
{
"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 " ".$jsondata->from; //zh
echo " ".$jsondata->trans_result[0]->src; //你好
?>
|
http://www.bkjia.com/PHPjc/733190.html
www.bkjia.com
true
http://www.bkjia.com/PHPjc/733190.html
TechArticle1.json_decode() json_decode (PHP 5 = 5.2.0, PECL json = 1.2.0) json_decode 对 JSON 格式的字符串进行编码 说明 mixed json_decode ( string $json [, bool $assoc ] ) 接受一...