我有这样的数据,这是在远程网址上:
{
"data": [
{
"id": "1234567890",
"name": "data_name",
"period": "data_period",
"values": [
{
"value": 52,
"end_time": "2013-01-21T08:00:00+0000"
},
{
"value": 54,
"end_time": "2013-01-22T08:00:00+0000"
},
{
"value": 57,
"end_time": "2013-01-23T08:00:00+0000"
},
{
"value": 58,
"end_time": "2013-01-24T08:00:00+0000"
}
],
"title": "Some title",
"description": "Some description"
}
]
}
有没有办法从链接到php变量?我对PHP有很好的了解,但我的javascript知识非常糟糕,所以我不知道如何实现这一点。从这一切来看,我实际上只需要“值”和“end_time”变量。
答案 0 :(得分:3)
此处不需要JavaScript。这是JSON表示法,PHP可以使用标准函数轻松解析。
试试这个:
$contents = file_get_contents($url); // You can also use cURL here if you like
$obj = json_decode($contents);
然后您可以将$var
视为对象。例如,$obj->data[0]->values
会返回包含value
和end_time
字段的对象数组:
Array
(
[0] => stdClass Object
(
[value] => 52
[end_time] => 2013-01-21T08:00:00+0000
)
[1] => stdClass Object
(
[value] => 54
[end_time] => 2013-01-22T08:00:00+0000
)
[2] => stdClass Object
(
[value] => 57
[end_time] => 2013-01-23T08:00:00+0000
)
[3] => stdClass Object
(
[value] => 58
[end_time] => 2013-01-24T08:00:00+0000
)
)