亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在 PHP 中回顯 json 數據

無法在 PHP 中回顯 json 數據

PHP
青春有我 2023-04-02 10:40:22
這是我的帶有 PHP 輸出的 Json 代碼$buffer = '[{"isSuccess":"true","message":"Consignment Number Valid","orderReferenceIdList":[":Succesfull"],"tracking_Details":[{"BookingDate":"02-05-2020","Destination":"MANDI BAHAUDDIN","Origin":"LAHORE","Shipper":"VESTURE MERCHANDISING (OTURE)","Consignee":"Syed Asad Abbas Bokhari","ServiceType":"Overnight","CNStatus":"DELIVERED","CNStatusID":"55","pieces":"1","weight":"0.5","Details":[{"DateTime":"5/5/2020 12:03:59 PM","Status":"Delivered","Location":"MANDI BAHAUDDIN","Detail":"Shipment has been Delivered. Delivery Date & Time May 5 2020 1:11AM and Received By: ASAD"},{"DateTime":"5/5/2020 10:03:58 AM","Status":"On Delivery","Location":"MANDI BAHAUDDIN","Detail":"Shipment is Out for Delivery."},{"DateTime":"5/4/2020 10:43:51 AM","Status":"On Delivery","Location":"MANDI BAHAUDDIN","Detail":"Shipment is Out for Delivery."},{"DateTime":"5/4/2020 10:06:57 AM","Status":"Unloading","Location":"MANDI BAHAUDDIN","Detail":"Shipment has arrived at hub."},{"DateTime":"5/4/2020 3:55:10 AM","Status":"Loading","Location":"GUJRANWALA","Detail":"Shipment has departed."},{"DateTime":"5/2/2020 11:20:44 PM","Status":"Unloading","Location":"Lahore Airport","Detail":"Shipment has arrived at hub."},{"DateTime":"5/2/2020 10:40:33 PM","Status":"Loading","Location":"KOT LAKH PAT","Detail":"Shipment has departed."},{"DateTime":"5/2/2020 8:30:52 PM","Status":"Arrived at OPS","Location":"LAHORE","Detail":"Shipment has arrived at origin facility."},{"DateTime":"5/2/2020 12:00:00 AM","Status":"Booking","Location":"LAHORE","Detail":"Shipment is booked."}]}]}]1';$data = json_decode($buffer, true);foreach ($data['tracking_Details'] as $a) {echo $a['BookingDate'];}這是一個JSON美化在線工具結果:https ://i.imgur.com/c5izkQH.png但是回聲的輸出什么也沒有。如何在 PHP 中輸出 JSON?
查看完整描述

2 回答

?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

首先,在你的 json 末尾有一個數字“1”,將其刪除。其次,因為你的 json 被包裹在一個 [] 中,所以在你解碼它之后,它將返回一個數組并且你的結果在元素 0 中所以你的代碼應該是:


$buffer = '[{"isSuccess":"true","message":"Consignment Number Valid","orderReferenceIdList":[":Succesfull"],"tracking_Details":[{"BookingDate":"02-05-2020","Destination":"MANDI BAHAUDDIN","Origin":"LAHORE","Shipper":"VESTURE MERCHANDISING (OTURE)","Consignee":"Syed Asad Abbas Bokhari","ServiceType":"Overnight","CNStatus":"DELIVERED","CNStatusID":"55","pieces":"1","weight":"0.5","Details":[{"DateTime":"5/5/2020 12:03:59 PM","Status":"Delivered","Location":"MANDI BAHAUDDIN","Detail":"Shipment has been Delivered. Delivery Date & Time May 5 2020 1:11AM and Received By: ASAD"},{"DateTime":"5/5/2020 10:03:58 AM","Status":"On Delivery","Location":"MANDI BAHAUDDIN","Detail":"Shipment is Out for Delivery."},{"DateTime":"5/4/2020 10:43:51 AM","Status":"On Delivery","Location":"MANDI BAHAUDDIN","Detail":"Shipment is Out for Delivery."},{"DateTime":"5/4/2020 10:06:57 AM","Status":"Unloading","Location":"MANDI BAHAUDDIN","Detail":"Shipment has arrived at hub."},{"DateTime":"5/4/2020 3:55:10 AM","Status":"Loading","Location":"GUJRANWALA","Detail":"Shipment has departed."},{"DateTime":"5/2/2020 11:20:44 PM","Status":"Unloading","Location":"Lahore Airport","Detail":"Shipment has arrived at hub."},{"DateTime":"5/2/2020 10:40:33 PM","Status":"Loading","Location":"KOT LAKH PAT","Detail":"Shipment has departed."},{"DateTime":"5/2/2020 8:30:52 PM","Status":"Arrived at OPS","Location":"LAHORE","Detail":"Shipment has arrived at origin facility."},{"DateTime":"5/2/2020 12:00:00 AM","Status":"Booking","Location":"LAHORE","Detail":"Shipment is booked."}]}]}]';


$data = json_decode($buffer, true);

$data = $data[0];

foreach ($data['tracking_Details'] as $a) {

    echo $a['BookingDate'];

}

這將返回


02-05-2020


查看完整回答
反對 回復 2023-04-02
?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

你的 json 格式是錯誤的。字符串的末尾有 1。我已將其刪除。試試下面的代碼


$buffer = '[{"isSuccess":"true","message":"Consignment Number Valid","orderReferenceIdList":[":Succesfull"],"tracking_Details":[{"BookingDate":"02-05-2020","Destination":"MANDI BAHAUDDIN","Origin":"LAHORE","Shipper":"VESTURE MERCHANDISING (OTURE)","Consignee":"Syed Asad Abbas Bokhari","ServiceType":"Overnight","CNStatus":"DELIVERED","CNStatusID":"55","pieces":"1","weight":"0.5","Details":[{"DateTime":"5/5/2020 12:03:59 PM","Status":"Delivered","Location":"MANDI BAHAUDDIN","Detail":"Shipment has been Delivered. Delivery Date & Time May 5 2020 1:11AM and Received By: ASAD"},{"DateTime":"5/5/2020 10:03:58 AM","Status":"On Delivery","Location":"MANDI BAHAUDDIN","Detail":"Shipment is Out for Delivery."},{"DateTime":"5/4/2020 10:43:51 AM","Status":"On Delivery","Location":"MANDI BAHAUDDIN","Detail":"Shipment is Out for Delivery."},{"DateTime":"5/4/2020 10:06:57 AM","Status":"Unloading","Location":"MANDI BAHAUDDIN","Detail":"Shipment has arrived at hub."},{"DateTime":"5/4/2020 3:55:10 AM","Status":"Loading","Location":"GUJRANWALA","Detail":"Shipment has departed."},{"DateTime":"5/2/2020 11:20:44 PM","Status":"Unloading","Location":"Lahore Airport","Detail":"Shipment has arrived at hub."},{"DateTime":"5/2/2020 10:40:33 PM","Status":"Loading","Location":"KOT LAKH PAT","Detail":"Shipment has departed."},{"DateTime":"5/2/2020 8:30:52 PM","Status":"Arrived at OPS","Location":"LAHORE","Detail":"Shipment has arrived at origin facility."},{"DateTime":"5/2/2020 12:00:00 AM","Status":"Booking","Location":"LAHORE","Detail":"Shipment is booked."}]}]}]';


$data = json_decode($buffer, true);

if(isset($data[0]['tracking_Details']) && !empty($data[0]['tracking_Details'])){        

    foreach ($data[0]['tracking_Details'] as $a) {

        echo $a['BookingDate'];

    }

}

輸出 :


02-05-2020


查看完整回答
反對 回復 2023-04-02
  • 2 回答
  • 0 關注
  • 181 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號