无法在 PHP 中回显 json 数据

这是我的带有 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?


青春有我
浏览 91回答 2
2回答

慕无忌1623718

首先,在你的 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

凤凰求蛊

你的 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
打开App,查看更多内容
随时随地看视频慕课网APP