为 JSON 多维数组创建表

Posted

技术标签:

【中文标题】为 JSON 多维数组创建表【英文标题】:Creating table for JSON multidimensional array 【发布时间】:2018-05-09 15:41:48 【问题描述】:

我的 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 创建一个表。情况是“deliveries”和“devices”数组的数量总是在变化,除了“orders”只有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

【问题讨论】:

【参考方案1】:

试试下面的代码,你需要在deliveries的当前循环中添加devices循环

$.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);
);

DEMO

【讨论】:

我确实尝试在交付循环中添加设备循环,但我使用的是 $.each(data.orders[0].deliveries.devices, function(key, value) 难怪它不会工作,因为它'将重新读取数据。非常感谢您的帮助! 很高兴帮助@KuSyafiq

以上是关于为 JSON 多维数组创建表的主要内容,如果未能解决你的问题,请参考以下文章

将多维 Json 数组解析为 Python

如何将多维数组转换为json对象[关闭]

如何从多维数组开始使用 DOM 创建表?

Laravel sync() 与多维数组

js如何操作多维json数组?

如何反序列化包含多维数组的json对象?