使用数据表获取嵌套的json数据

时间:2020-08-31 17:03:50

标签: jquery

我尝试过但每次出现错误时都找不到,请帮帮我, json数据:

{ "siteList": [ 
    { "siteName": "Site1", 
        "deviceList": [ 
            { "deviceName": "S1device1", "count": "1" }, 
            { "deviceName": "S1device2", "count": "2" } 
            ] 
    }, 
    { "siteName": "Site2", 
        "deviceList": [ 
            { "deviceName": "S2device1", "count": "1" }, 
            { "deviceName": "S2device2", "count": "2" } 
            ] 
    }, 
    { "siteName": "Site3", 
        "deviceList": [ 
            { "deviceName": "S3device1", "count": "1" }, 
            { "deviceName": "S3device2", "count": "2" } 
            ] 
    } 
    ] 
}

我想使用Datatable在html表格上显示它 这是html:

<table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Site Name</th>
                <th>Cabinet</th>
                <th>Count</th>
               
            </tr>
        </thead>
    </table>

还有Jquery:

 $(document).ready(function() {
         $('#example').DataTable( {
            "ajax": "user.json",
            "columns": [
                { "siteList": "siteName" },
                { "siteList": "deviceList:deviceName" },
                { "siteList": "deviceList:count" },
               
            ]
        } );
    } );

请帮助我做任何帮助,我们将不胜感激 预先感谢

1 个答案:

答案 0 :(得分:1)

user.json应该看起来不同。

在这里看看: https://datatables.net/examples/ajax/simple.html