简体   繁体   English

如何从JSON数据创建表

[英]How to create table from json data

JSON PART : JSON部分:

{
    "1": {"JAN": {"ID": "1","Name": "Jim"}},
    "2": {"FEB": {"ID": "2","Name": "Jack" } },
    "Idname" : "2",
    "Date" : "01/28/2014",
    "State" : "1"
}

I need the above json to create a table in the following format. 我需要上面的json以以下格式创建表。

HTML PART : HTML部分:

<table class="table">
<thead>
    <tr>
        <th>{{JAN}}</th>
    </tr>
</thead>                                
<tbody>
    <tr>
        <td>{{ID}}</td>
        <td>{{Name}}</td>
    </tr>
</tbody>
</table>

I need both header and body of table from json. 我需要json的表头和表体。 Also only one part of the whole json will be shown as table, rest will be shown as normal fields. 整个json的只有一部分将显示为表格,其余部分将显示为普通字段。

You need to use JSON.parse to parse data from the server. 您需要使用JSON.parse来解析服务器中的数据。

 s1 =  // required data store it here
 name: '1',
 data: JSON.parse("[" + s1 + "]")

Try this method 试试这个方法

for (var i in json) {
    if (typeof (json[i]) == "object") {
        var month = json[i];
        var headd='';
        var bodyy='';
        for (var j in month) {
            headd='<th>'+j+'</th>';
            var tbody = month[j];
            bodyy='<tr><td>'+ tbody.ID + '</td><td>' + tbody.Name+'</td><tr>';

        }
        $("#head_t").append(headd)
        $("#body_t").append(bodyy)
    }
}

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM