使用Javascript按月将JSON解析到表中
我很难将JSON对象解析为html表。基本上,我的JSON数据[EDITED]如下所示:使用Javascript按月将JSON解析到表中,javascript,jquery,arrays,json,html-table,Javascript,Jquery,Arrays,Json,Html Table,我很难将JSON对象解析为html表。基本上,我的JSON数据[EDITED]如下所示: [ { 'category': 'Automotive (Motor)', 'month':8, 'month_now':'Aug', 'year':2018, 'lists':{ 'total':1, 'price':591600 } }, { 'cat
[
{
'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
}
}
]
我想创建如下表:
[
{
'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
}
}
]
我想将月份从一月开始添加到Des,如果每个月没有总值,则使用空数据
我读了很多关于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);
}
});
$(文档).ready(函数(){
var json=[{'category':Automotive(Motor),'month':8,'month_now':'Aug','year':2018,'list':{'total':1,'price':591600},{'category':Health,'month_now':'Aug','month':8,'year':2018,'list':{'total':21,'price':14448600};
var-tr;
for(var i=0;i
以下是html:
<table>
<tr>
<th>Category</th>
<th>Month</th>
<th>Total</th>
</tr>
</table>
类别
月
全部的
total
属性$(文档).ready(函数(){
var json=[{
‘类别’:‘汽车(电机)’,
"月份":8,,
“现在的月份”:“八月”,
“年份”:2018年,
“列表”:{
“总计”:1,
“价格”:591600
}
}, {
“类别”:“健康”,
“现在的月份”:“八月”,
"月份":8,,
“年份”:2018年,
“列表”:{
总数:21,
“价格”:14448600
}
}];
var-tr;
for(var i=0;i
表格{
宽度:100%;
文本对齐:左对齐;
}
单位
J
F
M
A.
M
J
J
A.
s
O
N
D
根本原因:
您的json
无效。请首先确保您拥有有效的JSON。您可以使用任何方法来验证JSON
category
键有一个字符串值,因此该值必须包含在引号中,并且json[i]。列表
需要更改为json[i]。列表。总计
。
以下是工作演示:
$(文档).ready(函数(){
var json=[{
‘类别’:‘汽车(电机)’,
"月份":8,,
“现在的月份”:“八月”,
“年份”:2018年,
“列表”:{
“总计”:1,
“价格”:591600
}
}, {
“类别”:“健康”,
“现在的月份”:“八月”,
"月份":8,,
“年份”:2018年,
“列表”:{
总数:21,
“价格”:14448600
}
}];
var-tr;
for(var i=0;i
类别
月
全部的
@Zzet非常完美这只是一个改动
$(文档).ready(函数(){
var json=[{
‘类别’:‘汽车(电机)’,
"月份":8,,
“现在的月份”:“八月”,
“年份”:2018年,
“列表”:{
“总计”:1,
“价格”:591600
}
}, {
“类别”:“健康”,
“现在的月份”:“八月”,
"月份":8,,
“年份”:2018年,
“列表”:{
总数:21,
“价格”:14448600
}
}];
var-tr;
for(var i=0;i
表格{
宽度:100%;
文本对齐:左对齐;
}
单位
J
F
M
A.
M
J
J
A.
s
O
N
D
那么您现在面临什么问题呢?我想添加一个包含值的表在每个月。非常感谢,我得到了他们的帮助,Zze&Vicjordant Hank u我的朋友DDD:)非常感谢帮助和纠正我,我犯了一个错误,值必须是有效的JSON数据类型哦,mannn我非常感谢你考虑我的问题:D,它有效!