我需要将Json转换为数组,我需要阅读此数组

时间:2018-12-26 03:36:13

标签: javascript php json

我有这个Json,我需要用数组或其他读一本书,但我想将这些数据放在div或li中,你能帮我吗?

我正在尝试用php解决我的问题,但是我什么也没得到

{"meta":{"code":200,"type":"Success","message":"Success"},"data":{"id":"0a393bcdb0a9e069447040ed7a2b7f7b","tracking_number":"1Z762E1W6842003229","carrier_code":"ups","status":"delivered","created_at":"2018-12-24T18:13:03+00:00","updated_at":"2018-12-24T21:58:44+00:00","order_create_time":null,"customer_email":"","title":"","order_id":null,"comment":null,"customer_name":null,"archived":false,"original_country":"Poland","destination_country":"Germany","itemTimeLength":42,"stayTimeLength":62,"origin_info":{"ReferenceNumber":null,"ItemReceived":"2018-09-12 21:50","ItemDispatched":null,"DepartfromAirport":null,"ArrivalfromAbroad":null,"CustomsClearance":null,"DestinationArrived":null,"weblink":"http:\/\/www.ups.com\/content\/pl\/en\/contact\/index.html?WT.svl=Footer","phone":"+48-22-534-00-00","carrier_code":"ups","trackinfo":[{"Date":"2018-10-24 13:01","StatusDescription":"Were attempting to verify the package location. \/ Investigation only","Details":"ROCKFIELD,MD,20850,US","checkpoint_status":"transit"},{"Date":"2018-09-14 09:56","StatusDescription":"Delivered","Details":"DORTMUND,44145,DE","checkpoint_status":"delivered"},{"Date":"2018-09-14 07:26","StatusDescription":"Out For Delivery","Details":"Herne-Boernig,DE","checkpoint_status":"pickup"},{"Date":"2018-09-14 05:00","StatusDescription":"Arrival Scan","Details":"Herne-Boernig,DE","checkpoint_status":"transit"},{"Date":"2018-09-14 02:14","StatusDescription":"Departure Scan","Details":"Langenhagen,DE","checkpoint_status":"transit"},{"Date":"2018-09-13 16:52","StatusDescription":"Arrival Scan","Details":"Langenhagen,DE","checkpoint_status":"transit"},{"Date":"2018-09-13 04:05","StatusDescription":"Departure Scan","Details":"Dobra Strykow,PL","checkpoint_status":"transit"},{"Date":"2018-09-13 01:40","StatusDescription":"Arrival Scan","Details":"Dobra Strykow,PL","checkpoint_status":"transit"},{"Date":"2018-09-13 00:13","StatusDescription":"Departure Scan","Details":"Wolka Kosowska,PL","checkpoint_status":"transit"},{"Date":"2018-09-12 21:50","StatusDescription":"Origin Scan","Details":"Wolka Kosowska,PL","checkpoint_status":"transit","ItemNode":"ItemReceived"},{"Date":"2018-08-11 17:17","StatusDescription":"Order Processed: Ready for UPS","Details":"PL","checkpoint_status":"transit"}]},"service_code":"UPS Standard","weight":"22.00KGS","substatus":null,"lastEvent":"Were attempting to verify the package location. \/ Investigation only,ROCKFIELD,MD,20850,US,2018-10-24 13:01","lastUpdateTime":"2018-10-24 13:01"}}

我什么都看不到(数据,元数据,日期)。

我希望得到相同的输出:

<div id="data">
<li>id: 0a393bcdb0a9e069447040ed7a2b7f7b</li>
<li>Tracking_number: 1Z762E1W6842003229 </li>
... 
</div>

<br>
<div id="dates">
<li>Date: 2018-09-13 16:52</li>
<li>StatusDescription": :"Departure Scan" </li> ,
<li>Details": Dobra Strykow,PL </li>
<li>checkpoint_status": transit </li>
...
</div>
<hr>

<div id="dates>
other date[2];
...
</div>
<hr>

...

1 个答案:

答案 0 :(得分:0)

您可以整理模板中的一些HTML。但是你去了:

var dataObj = {
  "meta": {
    "code": 200,
    "type": "Success",
    "message": "Success"
  },
  "data": {
    "id": "0a393bcdb0a9e069447040ed7a2b7f7b",
    "tracking_number": "1Z762E1W6842003229",
    "carrier_code": "ups",
    "status": "delivered",
    "created_at": "2018-12-24T18:13:03+00:00",
    "updated_at": "2018-12-24T21:58:44+00:00",
    "order_create_time": null,
    "customer_email": "",
    "title": "",
    "order_id": null,
    "comment": null,
    "customer_name": null,
    "archived": false,
    "original_country": "Poland",
    "destination_country": "Germany",
    "itemTimeLength": 42,
    "stayTimeLength": 62,
    "origin_info": {
      "ReferenceNumber": null,
      "ItemReceived": "2018-09-12 21:50",
      "ItemDispatched": null,
      "DepartfromAirport": null,
      "ArrivalfromAbroad": null,
      "CustomsClearance": null,
      "DestinationArrived": null,
      "weblink": "http:\/\/www.ups.com\/content\/pl\/en\/contact\/index.html?WT.svl=Footer",
      "phone": "+48-22-534-00-00",
      "carrier_code": "ups",
      "trackinfo": [{
        "Date": "2018-10-24 13:01",
        "StatusDescription": "Were attempting to verify the package location. \/ Investigation only",
        "Details": "ROCKFIELD,MD,20850,US",
        "checkpoint_status": "transit"
      }, {
        "Date": "2018-09-14 09:56",
        "StatusDescription": "Delivered",
        "Details": "DORTMUND,44145,DE",
        "checkpoint_status": "delivered"
      }, {
        "Date": "2018-09-14 07:26",
        "StatusDescription": "Out For Delivery",
        "Details": "Herne-Boernig,DE",
        "checkpoint_status": "pickup"
      }, {
        "Date": "2018-09-14 05:00",
        "StatusDescription": "Arrival Scan",
        "Details": "Herne-Boernig,DE",
        "checkpoint_status": "transit"
      }, {
        "Date": "2018-09-14 02:14",
        "StatusDescription": "Departure Scan",
        "Details": "Langenhagen,DE",
        "checkpoint_status": "transit"
      }, {
        "Date": "2018-09-13 16:52",
        "StatusDescription": "Arrival Scan",
        "Details": "Langenhagen,DE",
        "checkpoint_status": "transit"
      }, {
        "Date": "2018-09-13 04:05",
        "StatusDescription": "Departure Scan",
        "Details": "Dobra Strykow,PL",
        "checkpoint_status": "transit"
      }, {
        "Date": "2018-09-13 01:40",
        "StatusDescription": "Arrival Scan",
        "Details": "Dobra Strykow,PL",
        "checkpoint_status": "transit"
      }, {
        "Date": "2018-09-13 00:13",
        "StatusDescription": "Departure Scan",
        "Details": "Wolka Kosowska,PL",
        "checkpoint_status": "transit"
      }, {
        "Date": "2018-09-12 21:50",
        "StatusDescription": "Origin Scan",
        "Details": "Wolka Kosowska,PL",
        "checkpoint_status": "transit",
        "ItemNode": "ItemReceived"
      }, {
        "Date": "2018-08-11 17:17",
        "StatusDescription": "Order Processed: Ready for UPS",
        "Details": "PL",
        "checkpoint_status": "transit"
      }]
    },
    "service_code": "UPS Standard",
    "weight": "22.00KGS",
    "substatus": null,
    "lastEvent": "Were attempting to verify the package location. \/ Investigation only,ROCKFIELD,MD,20850,US,2018-10-24 13:01",
    "lastUpdateTime": "2018-10-24 13:01"
  }
};

const packagePanel = (package) => {
  let newPackage = document.createElement("div");
  newPackage.innerHTML = packageData(package) + packageDates(package.origin_info.trackinfo);
  return newPackage;
  
};

const packageData = (package) => {
  return `<ul><li> id: ${package.id}</li> 
  <li> Tracking_number: ${package.tracking_number}</li></ul>`;
};

const packageDates = (dateArr) => {
  let html = '';
  dateArr.forEach(date => {
    html += `<ul><li> ${date.Date} </li> 
             <li> Status Descriuption ${date.StatusDescription} </li>  
             <li> Details : ${date.Details} </li>
             <li> Checkpoint status : ${date.checkpoint_status} </li></ul>`;
  });
  return html;
};

document.addEventListener('DOMContentLoaded', function(){
  console.log('prep');
    document.getElementById("data").appendChild(packagePanel(dataObj.data));
});


 
<div id="data">

</div>