我的JSON在这里:
{
"orders": [
{
"orderNumber": "ORDER_900123",
"orderDate": "2014-08-28T17:10:10Z",
"orderType": "OR",
"customerId": "19827",
"poNumber": "PO_12345",
"deliveries": [
{
"deliveryNumber": "D1.2",
"shipDate": "2014-10-10T12:10:00Z",
"devices": [
{
"deviceId": "1111",
"assetTag": "A123456"
},
{
"deviceId": "2222",
"assetTag": "A123456"
}
]
},
{
"deliveryNumber": "D2.2",
"shipDate": "2019-10-10T12:10:00Z",
"devices": [
{
"deviceId": "3333",
"assetTag": "A123456"
},
{
"deviceId": "4444",
"assetTag": "A123456"
}
]
}
]
}
],
"statusCode": "COMPLETE",
"respondedOn": "2014-10-17T07:22:53Z"
}
我正在尝试使用$ .each循环为此JSON创建一个表。情况是“交付”和“设备”数组的数量总是会改变,除了“订单”,它只是1个数组。
这是我到目前为止所拥有的,因为“orders”数组将始终为1我只需将它们的键和值放在var中并稍后回显:
$.getJSON("sodresponse.json", function(data){
var statusCode = data.statusCode;
var respondedOn = data.respondedOn;
var orderNumber = data.orders[0].orderNumber;
var orderDate = data.orders[0].orderDate;
var orderType = data.orders[0].orderType;
var customerId = data.orders[0].customerId;
var deliveries = '';
var devices = '';
$.each(data.orders[0].deliveries, function(key, value){
deliveries += '<tr>';
deliveries += '<th>Delivery No:</th>';
deliveries += '<td>'+value.deliveryNumber+'</td>';
deliveries += '</tr>';
deliveries += '<tr>';
deliveries += '<th>Ship Date:</th>';
deliveries += '<td>'+value.shipDate+'</td>';
deliveries += '</tr>';
deliveries += '<tr>';
deliveries += '<th>Device ID:</th>';
deliveries += '<td>'+value.devices.deviceId+'</td>';
deliveries += '</tr>';
deliveries += '<tr>';
deliveries += '<th>Asset Tag:</th>';
deliveries += '<td>'+value.devices.assetTag+'</td>';
deliveries += '</tr>';
});
$.each(data.orders[0].deliveries.devices, function(key, value){
devices += '<tr>';
devices += '<th>Device ID</th>';
devices += '<td>'+value.deviceId+'</td>';
devices += '</tr>';
devices += '<tr>';
devices += '<th>Asset Tag</th>';
devices += '<td>'+value.assetTag+'</td>';
devices += '</tr>';
});
});
$('#sodtable').append(deliveries);
我想要的输出是这样的:
Delivery No: D1.2
Ship Date: 2014-10-10T12:10:00Z
Device ID: 1111
Asset Tag: A123456
Device ID: 2222
Asset Tag: A123456
Delivery No: D.2
Ship Date: 2019-10-10T12:10:00Z
Device ID: 3333
Asset Tag: A123456
Device ID: 4444
Asset Tag: A123456
答案 0 :(得分:3)
尝试以下代码,您需要在devices
的当前循环中添加deliveries
循环
$.getJSON("sodresponse.json", function(data){
var statusCode = data.statusCode;
var respondedOn = data.respondedOn;
var orderNumber = data.orders[0].orderNumber;
var orderDate = data.orders[0].orderDate;
var orderType = data.orders[0].orderType;
var customerId = data.orders[0].customerId;
var deliveries = '';
var devices = '';
$.each(data.orders[0].deliveries, function(key, value){
deliveries += '<tr>';
deliveries += '<th>Delivery No:</th>';
deliveries += '<td>'+value.deliveryNumber+'</td>';
deliveries += '</tr>';
deliveries += '<tr>';
deliveries += '<th>Ship Date:</th>';
deliveries += '<td>'+value.shipDate+'</td>';
deliveries += '</tr>';
$.each(value.devices, function(key1, value1){
console.log(value1.deviceId);
deliveries += '<tr>';
deliveries += '<th>Device ID</th>';
deliveries += '<td>'+value1.deviceId+'</td>';
deliveries += '</tr>';
deliveries += '<tr>';
deliveries += '<th>Asset Tag</th>';
deliveries += '<td>'+value1.assetTag+'</td>';
deliveries += '</tr>';
});
deliveries += '<tr><td colspan="2"></td></tr>';
});
$('#sodtable').append(deliveries);
});