我正在从 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;
}
?>
我的最终目标是列出唯一的名称列表,然后我可以将其显示在备用网页中。
所以我的问题是......我如何阅读这个页面并很好地格式化数据(也许我可以打印一个数组?)以便我将来可以使用它?