简体   繁体   English

如何使用Javascript将嵌套JSON映射到HTML表

[英]How to map Nested JSON to an HTML table using Javascript

I have JSON Data from an API which contains users at different locations. 我有一个来自API的JSON数据,该API包含位于不同位置的用户。 I want to map it to an HTML table that looks like the one shared below. 我想将其映射到看起来像下面共享的HTML表。

HTML TABLE: HTML表格: HTML表格

JSON DATA: JSON数据:

   {
    "data": [{

        "location": "Location 1",
        "Address": "location 1, Address",
        "users": [{
            "name": "Location 1 User",
            "state": "Active"
        }]

    }, {
        "location": "Location 2",
        "Address": "Location 2, Address",


        "users": [{
                "name": "Location 2 User",
                "state": "Active"
            },
            {
                "name": "Location 2 User",
                "state": "Inactive"
            }
        ]

    }]
}

If you notice in the third column, both users of the second location are added inside one td element and are listed in one cell. 如果您在第三列中注意到,则第二个位置的两个用户都被添加到一个td元素内,并在一个单元格中列出。 I want to transform my JSON to this sort of table with RAW Javascript. 我想用RAW Javascript将JSON转换为这种表。

I have tried different ways but no luck so any help on this would be appreciated! 我尝试了不同的方法,但是没有运气,因此,对此将有所帮助!

Thank you! 谢谢!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th colspan="2">Location Name</th>
                <th colspan="2">Address</th>
                <th colspan="2">User Working</th>
            </tr>
        </thead>

        <tbody>

        </tbody>
    </table>
</body>
</html>

<script>
    json_data = 
        {
            "data": [{

                "location": "Location 1",
                "Address": "location 1, Address",
                "users": [{
                    "name": "Location 1 User",
                    "state": "Active"
                }]

            }, {
                "location": "Location 2",
                "Address": "Location 2, Address",


                "users": [{
                        "name": "Location 2 User",
                        "state": "Active"
                    },
                    {
                        "name": "Location 2 User",
                        "state": "Inactive"
                    }
                ]

            }]
        } 
    //Accessing the JSON Document using these loops
    Object.keys(json_data['data']).forEach(key => {
        var tr = document.createElement('tr');
        tr.className = 'cell'
        document.getElementsByTagName('tbody')[0].appendChild(tr);
        Object.keys(json_data['data'][key]).forEach(key2 => {
            var td = document.createElement('td');
            td.setAttribute('colspan', '2');
            if (key2 === 'users') {
                Object.keys(json_data['data'][key][key2]).forEach(key3 => {
                    if (key3 > 0) {
                        td.innerHTML = td.innerHTML + ', ' + json_data['data'][key][key2][key3]['name'];
                    }
                    else {
                        td.innerHTML = td.innerHTML + json_data['data'][key][key2][key3]['name'];
                    }

                })
            }
            else {
                td.innerHTML = json_data['data'][key][key2];
            }
            document.getElementsByClassName('cell')[key].appendChild(td);
        })
    });


</script>

the code is pretty straightforward. 该代码非常简单。 The main thing to take note of is the Object.keys().forEach() loops and the createElements 要注意的主要事情是Object.keys()。forEach()循环和createElements

There could be a much easier way to do this, but hey, it works. 可能有一种更简单的方法来执行此操作,但是,它确实有效。

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

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