【问题标题】:How to access multi-level json tree in PHP for database insertion如何在 PHP 中访问多级 json 树以进行数据库插入
【发布时间】: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'];

}
?>

正如您在上面看到的,我尝试了几种方法来访问正在显示的更深层次的数据,但无济于事。

我目前收到“试图获取非对象的属性”的错误。怎么可能到达这个数组中的数据?

【问题讨论】:

    标签: php json


    【解决方案1】:

    尝试:

    echo $results['results']['collection'][0]['textlink']['href'];
    

    【讨论】:

    • 谢谢,但这只会返回第一个结果。如何编辑 foreach 循环以显示所有结果?
    • @Liam-FD 你正在循环通过$results 的第一级,这是你的json 中的所有内容。考虑循环为foreach ($results['results']['collection'] as $item),然后使用$item['index']$item['textlink']['href']等等。
    • 类似这样的://echo '
      '; foreach($results['results']['collection'] as $item) { //echo print_r($item);回声 $item['textlink']['href'];您可以取消注释这些行以获得预格式化版本以查看您可以定位的键
    【解决方案2】:
    $obj = json_decode( $json, true ); foreach ( $obj['key'] as $key => $value ) { echo $value; }
    

    【讨论】:

      【解决方案3】:
      foreach ($response['results']['collection'] as $textlink) {
       $row = $textlink['textlink'];
       echo $row['href'];
       echo "<br>";
       echo $row['text'];
       echo "<br>";
      }
      

      你可以这样做 foreach ,它只循环集合中的项目

      【讨论】:

        【解决方案4】:

        我建议做这样的事情(根据我的说法,从 API 响应中获取结果的正确方法):

        <?php
        $request = "API.REQUEST.NET";
        $response = file_get_contents($request);
        
        $response = json_decode($response);
        
        foreach($response->results->collection as $item) {
        
        echo $item->textlink->href;
        echo "<br>";
        echo $item->textlink->text;
        echo "<br>";
        echo $item->index;
        echo "<br>";
        echo $item->url;
        
        }
        ?>
        

        【讨论】:

        • 这仍然会在这一行返回一个 Trying to get property of non-object 错误foreach($response-&gt;results-&gt;collection as $item) {
        • 如果响应是有效对象,你应该可以获取它,你能显示响应的var_dump结果吗?
        • 检查更新的答案。响应现在应该是对象。
        猜你喜欢
        • 2019-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-01
        • 2021-09-30
        • 2017-10-24
        • 1970-01-01
        • 2019-08-17
        相关资源
        最近更新 更多