循环遍历 JSON 失败,并出现json_decode


Looping through JSON fails with json_decode

>我正在从URL中提取JSON数据并尝试仅显示显示名称的列表。

在某种程度上,如果我知道每次都会返回 X 个结果,这也很容易循环。但是,返回的结果将在 0 到 50+ 之间变化。

我做了很多搜索,都说"只用json_decode"......事实并非如此。

我有以下 JSON:

{
    "ACK": "SUCCESS",
    "ERROR": null,
    "AGENT": {
        "has_results": 1,
        "agents": [
            {
                "display_name": "Alex",
                "time_in_state": "5214",
                "state": "Aux",
                "callstakentoday": null,
                "callsouttoday": null,
                "agntpriority": null,
                "skill_num": "76"
            },
            {
                "display_name": "Bill",
                "time_in_state": "6312",
                "state": "Aux",
                "callstakentoday": null,
                "callsouttoday": null,
                "agntpriority": null,
                "skill_num": "76"
            },
            {
                "display_name": "Carrie",
                "time_in_state": "5982",
                "state": "Aux",
                "callstakentoday": null,
                "callsouttoday": null,
                "agntpriority": null,
                "skill_num": "76"
            },
            {
                "display_name": "David",
                "time_in_state": "4226",
                "state": "Aux",
                "callstakentoday": null,
                "callsouttoday": null,
                "agntpriority": null,
                "skill_num": "76"
            }
        ]
    },
    "SKILL": {
        "has_results": 1,
        "num_skills": 1,
        "skills": [
            {
                "display_name": "Phone Skills",
                "skill_num": "76",
                "callsinqueue": "0",
                "callstoday": "9",
                "abandtoday": "0",
                "lwt": "0",
                "ewt": "0",
                "servicelvl": "100",
                "avgspeedans": "6",
                "talktime": "289"
            }
        ]
    },
    "TIME": 1383766541
}

从我阅读的示例和文档中,已创建以下代码:

<?php
    $url="http://myjsonurl.local";
    $json = file_get_contents($url);
    //echo $json; 
    $json = json_decode($json);
    foreach($json as $item->display_name)
    {
            echo $item->agents->display_name;
    }
?>

我的最终目标是拥有一个仅包含名称的列表,然后我可以在备用网页中显示这些名称。

所以我的问题是...如何阅读此页面并很好地格式化数据(也许是我可以打印的数组?),以便将来可以使用它?

代码中有以下内容:

foreach($json as $item->display_name)    

这是不正确的,不会做你想要的。正如你通过执行print_r($json)看到的,名称是$json->AGENT->agents的,所以你需要遍历这些项目,然后使用箭头语法($item->display_name)遍历display_name。获得显示名称后,可以将其推送到数组中,并根据需要使用它。

您的循环应如下所示:

$names = array(); // initialize empty array
foreach($json->AGENT->agents as $item)
{
    $names[] = $item->display_name;
}
print_r($names); // see the array contents

输出:

Array
(
    [0] => Alex
    [1] => Bill
    [2] => Carrie
    [3] => David
)

演示。

注意:如果您事先不知道 JSON 对象的结构,则可以使用嵌套foreach循环来检索名称。

要迭代的数组是 $json->AGENT->agents 。此外,您的foreach语法是错误的。

尝试:

foreach($json->AGENT->agents as $item)
{
        echo $item->display_name;
}
如果要

将 JSON 数据作为数组而不是对象获取,请改用json_decode($json, true)。这告诉函数将其作为关联数组返回,您可以在此处阅读:http://php.net/manual/en/function.json-decode.php

你的 foreach 循环中有一个错误。

foreach($json as $item->display_name)
    {
            echo $item->agents->display_name;
    }

$item 不是一个对象,您实际上正在尝试在此行中访问 none 对象的属性"display_name" ($item)

foreach($json as $item)