为了账号安全,请及时绑定邮箱和手机立即绑定

无法在 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 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信