怎樣使用php擷取複雜json資料並輸出,該如何解決

來源:互聯網
上載者:User
怎樣使用php擷取複雜json資料並輸出
這是調用多客服查看當前線上客服時的api

返回資料樣本(正確時的JSON返回結果):
{
"kf_online_list": [
{
"kf_account": "[email protected]",
"status": 1,
"kf_id": "1001",
"auto_accept": 0,
"accepted_case": 1
},
{
"kf_account": "[email protected]",
"status": 1,
"kf_id": "1002",
"auto_accept": 0,
"accepted_case": 2
}
]
}


以下是My Code:

$url_online ="https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token={$access_token}";
$output_online=file_get_contents($url_online);
$jsonStr = json_decode($output_online,true);
//讀取資訊:
$content = "客服1:".$jsonStr["kf_online_list"]["kf_account"]."\n線上狀態:".$jsonStr["kf_online_list"]["status"]."\n正在接待人數:".$jsonStr["kf_online_list"]["accepted_case"]."\n\n客服2:".$jsonStr["kf_online_list"]["kf_account"]."\n線上狀態:".$jsonStr["kf_online_list"]["status"]."\n正在接待人數:".$jsonStr["kf_online_list"]["accepted_case"];
?>


我本是希望獲得所有的kf_account資料,並返回到回話介面,可是這結構有點複雜,還有相同的欄位。。。
目前還查詢不到結果,我估計是最後擷取json的數組寫的不對,希望大家幫忙!
------解決思路----------------------
$s =<<< JSON
{
"kf_online_list": [
{
"kf_account": "[email protected]",
"status": 1,
"kf_id": "1001",
"auto_accept": 0,
"accepted_case": 1
},
{
"kf_account": "[email protected]",
"status": 1,
"kf_id": "1002",
"auto_accept": 0,
"accepted_case": 2
}
]
}
JSON;
$jsonStr = json_decode($s, true);

$content = '';
foreach($jsonStr['kf_online_list'] as $i=>$item) {
$n = $i + 1;
$content .= "
客服{$n}:$item[kf_account]
線上狀態:$item[status]
正在接待人數:$item[accepted_case]
";
}
echo $content;

客服1:[email protected]
線上狀態:1
正在接待人數:1

客服2:[email protected]
線上狀態:1
正在接待人數:2

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.