2 回答
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
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
- 2 回答
- 0 关注
- 150 浏览
添加回答
举报