duansang8388 2013-08-22 03:25
浏览 26

JSON到HTML表

So before I start off, I'd like to say that I only know basic HTML and CSS. Now that that's out of the way, I have a JSON output at http://eu.bitswit.ch/api/server_leaderboard.php?server=71 and I would like to put that into HTML tables. I have looked around on Google/Youtube but none of them were in-depth enough to help me.

[{"nickname":"|Gates|Domon","steam_id":"STEAM_0:1:8647245","kills":379,"deaths":175,"suicides":0,"tks":5,"score":4590},{"nickname":"Ambassador Pineapple","steam_id":"STEAM_0:1:5287117","kills":372,"deaths":127,"suicides":0,"tks":2,"score":4500},{"nickname":"Clayton","steam_id":"STEAM_0:1:57875311","kills":307,"deaths":118,"suicides":0,"tks":6,"score":3595},{"nickname":"Fluffy Penguin","steam_id":"STEAM_0:1:40834109","kills":205,"deaths":136,"suicides":0,"tks":5,"score":1620},
  • 写回答

2条回答 默认 最新

  • dqingn8836 2013-08-22 03:36
    关注

    I would use something like this (you need jQuery of some version to use this code though):

    if (data.length > 0) {
        var $table = $("<table/>", { "class": "myTable" }).appendTo(document);
    
        var $headRow = $("<tr/>", { "class": "myHeadRow" }).appendTo($table);
        for (var val in data[0]) {
            var $headCell = $("<td/>", {
                "class": "myHeadCell",
                "text": val
            }).appendTo($headRow);
        }
    
        for (var i = 0; i < data.length; i++) {
            var $row = $("<tr/>", { "class": "myRow" }).appendTo($table);
            for (var val in data[i]) {
                var $cell = $("<td/>", {
                    "class": "myCell",
                    "text": data[i][val]
                }).appendTo($row);
            }
        }
    }
    

    But notice, it's not very fast way to do. I'd recommend you to get JSON on your server and manupulate with it from your PHP or C# code or whatever and then just show rendered table. It is bad tone in web, when page load speed depends on client's computing powers.

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决