【发布时间】:2015-07-26 14:16:09
【问题描述】:
我正在访问返回 JSON 数据的 API,如下所示。
{
"name": "",
"count": 4,
"frequency": "",
"version": 3,
"newdata": true,
"lastrunstatus": "success",
"thisversionstatus": "success",
"thisversionrun": "",
"results": {
"collection": [{
"textlink": {
"href": "http://text1.com",
"text": "Text 1"
},
"index": 1,
"url": ""
}, {
"textlink": {
"href": "http://text2.com",
"text": "Text 2"
},
"index": 2,
"url": ""
}, {
"textlink": {
"href": "http://text3.com",
"text": "Text 3"
},
"index": 3,
"url": ""
}, {
"textlink": {
"href": "http://text4.com",
"text": "Text 4"
},
"index": 4,
"url": ""
}]
}}
如您所见,返回的 JSON 树具有多步级别。我希望能够在 PHP 中获得一些更深层次的内容,然后插入数据库。
目前我正在使用此代码来尝试回显数据(一旦我能够,我就可以将其插入数据库没有问题)
<?php
$request = "API.REQUEST.NET";
$response = file_get_contents($request);
$results = json_decode($response, TRUE);
foreach($results as $item) {
echo $item->results[0]->collection[0]->textlink[0]->href;
echo "<br>";
echo $item->results->collection['href'];
echo "<br>";
echo $item->results->collection['text'];
}
?>
正如您在上面看到的,我尝试了几种方法来访问正在显示的更深层次的数据,但无济于事。
我目前收到“试图获取非对象的属性”的错误。怎么可能到达这个数组中的数据?
【问题讨论】: