2013-08-22 43 views
0

因此,在我开始之前,我想说我只知道基本的HTML和CSS。 既然这样,我在http://eu.bitswit.ch/api/server_leaderboard.php?server=71有一个JSON输出,我想把它放到HTML表格中。 我在Google/Youtube上浏览过,但没有一篇深入到足以帮助我。JSON到HTML表

[{"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}, 
+0

请提供您的JSON(或短,足够的例子)到问题本身。 – Vulcan

+1

堆栈溢出就像一个图书馆为未来的访客。这就是代码需要提出问题的原因。要做到这一点,点击编辑,然后复制/粘贴。除非是某人的版权数据,否则不要这样做。谢谢。 – Paul

+0

点击编辑,而不是评论。 – Paul

回答

0

我会用这样的事情(你需要的jQuery的一些版本,但使用此代码):

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); 
     } 
    } 
} 

但注意,这不是很快捷的方式做。我建议你在你的服务器上获取JSON,并从你的PHP或C#代码或其他任何东西中进行处理,然后显示渲染表。 当网页加载速度取决于客户端的计算能力时,它在网络中很糟糕。从问题的URL

1

JSON例如:http://eu.bitswit.ch/api/server_leaderboard.php?server=71

{"query":"71","response":0,"query_time":"402.04ms","data":[{"nickname":"Gates Domon","steam_id":"STEAM_0:1:8647245","kills":380,"deaths":175,"suicides":0,"tks":5,"score":4595}]} 

$json = file_get_contents(' http://eu.bitswit.ch/api/server_leaderboard.php?server=71'); // this WILL do an http request for you 
    $data = json_decode($json, true); 
    $array = $data['data']; 
    $table = "<table cellpadding='5'> 
<thead> 
    <th>nickname</th> 
    <th>steam_id</th> 
    <th>kills</th> 
    <th>deaths</th> 
    <th>suicides</th> 
    <th>tks</th> 
    <th>score</th> 
</thead> 
<tbody>"; 
    foreach ($array as $value) { 
     $table .="<tr> 
<td>{$value['nickname']}</td> 
<td>{$value['steam_id']}</td> 
<td>{$value['kills']}</td> 
<td>{$value['deaths']}</td> 
<td>{$value['suicides']}</td> 
<td>{$value['tks']}</td> 
<td>{$value['score']}</td> 
</tr>"; 
    } 
    $table .="</tbody></table>"; 

    echo $table;