【发布时间】:2019-02-08 02:52:36
【问题描述】:
我很难将 JSON 对象解析为 html 表。基本上我的 JSON 数据 [EDITED] 看起来像这样:
[
{
'category': 'Automotive (Motor)',
'month':8,
'month_now':'Aug',
'year':2018,
'lists':{
'total':1,
'price':591600
}
},
{
'category': 'Health',
'month_now':'Aug',
'month':8,
'year':2018,
'lists':{
'total':21,
'price':14448600
}
}
]
我想创建一个这样的表:
我想添加从 Jan 到 Des 的月份,如果每个月都没有总值,则使用 null 数据。
我已经阅读了很多关于 JSON 格式的知识,但我的知识非常有限,我需要帮助-__-
我正在尝试,这是我的代码:
$(document).ready(function () {
var json = [{'category': Automotive (Motor),'month': 8, 'month_now': 'Aug', 'year': 2018, 'lists': {'total': 1, 'price': 591600}}, {'category': Health, 'month_now': 'Aug', 'month': 8, 'year': 2018, 'lists': {'total': 21, 'price': 14448600}}];
var tr;
for (var i = 0; i < json.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + json[i].category + "</td>");
tr.append("<td>" + json[i].month + "</td>");
tr.append("<td>" + json[i].lists + "</td>");
$('table').append(tr);
}
});
这是html:
<table>
<tr>
<th>Category</th>
<th>Month</th>
<th>Total</th>
</tr>
</table>
【问题讨论】:
-
那么你现在面临什么问题?
-
我想添加一个包含每个月的值的表。非常感谢,我得到了他们 Zze 和 VicJordan 的帮助
标签: javascript jquery arrays json html-table