这类似于this question,但我认为为了让自己更清楚,我会改写它有点不同。 我让这个json从$ .ajax调用回来了:
{ “列”:[ “PERSONID”, “名字”, “LASTNAME”], “DATA”:[[1001, “斯科特”, “温默”],[1002, “菲利普”, “的Senn”] [1003 “保”, “尼尔森”]]}
问:在JavaScript中,如何解析它以创建如下表:
<table>
<thead>
<tr>
<th>PersonID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1001</td>
<td>Scott</td>
<td>Wimmer</td>
</tr>
<tr>
<td>1002</td>
<td>Phillip</td>
<td>Senn</td>
</tr>
<tr>
<td>1003</td>
<td>Paul</td>
<td>Nielsen</td>
</tr>
</tbody>
</table>
答案 0 :(得分:3)
var yourJson = {"COLUMNS":["PERSONID","FIRSTNAME","LASTNAME"],"DATA":[[1001,"Scott","Wimmer"],[1002,"Phillip","Senn"],[1003,"Paul","Nielsen"]];
var table = '<table>';
table += '<thead><tr><th>' + yourJson.COLUMNS.join('</th><th>') + '</th></tr></thead>';
table += '<tbody>';
for (var i=0;i<yourJson.DATA.length;i++) {
table += '<tr><td>' + yourJson.DATA[i].join('</td><td>') + '</td></tr>';
};
table += '</tbody>';
table += '</table>';
答案 1 :(得分:2)
您可以使用jTemplates或pure等客户端模板引擎轻松实现。