Javascript 通过变量访问json对象值
我想访问json对象的值。每次获取json对象时,json对象的字段名都会不同,因此我将分别在xAxis和yAxis中发送字段名 当我试图访问jsonData.data[0].data[0].xAxis时,它给了我未定义的值,而不是值。Javascript 通过变量访问json对象值,javascript,json,object,Javascript,Json,Object,我想访问json对象的值。每次获取json对象时,json对象的字段名都会不同,因此我将分别在xAxis和yAxis中发送字段名 当我试图访问jsonData.data[0].data[0].xAxis时,它给了我未定义的值,而不是值。 我不能这样访问,因为每次字段名都会不同,所以我尝试通过变量jsonData.data[0].data[0].Part来访问它。对于xAxis,这应该可以: { "data": [{ "data": [{ "Part": "1.75 L I
我不能这样访问,因为每次字段名都会不同,所以我尝试通过变量jsonData.data[0].data[0].Part来访问它。对于xAxis,这应该可以:
{
"data": [{
"data": [{
"Part": "1.75 L ICON (Glass)",
"ProductionCounts": 1012620
}, {
"Part": "1.75 L Marg Mix (PET)",
"ProductionCounts": 1003278
}, {
"Part": "1.75 L Authentics (PET)",
"ProductionCounts": 457615
}, {
"Part": "1.0 L Margarita Mix / PET",
"ProductionCounts": 660982
}, {
"Part": "other",
"ProductionCounts": 1571985
}]
}, ],
"dateArray": ["2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01"],
"xAxis": "Part",
"yAxis": "ProductionCounts",
"interestingMoments": []
}
请参见此处:对于xAxis,这应该可以:
{
"data": [{
"data": [{
"Part": "1.75 L ICON (Glass)",
"ProductionCounts": 1012620
}, {
"Part": "1.75 L Marg Mix (PET)",
"ProductionCounts": 1003278
}, {
"Part": "1.75 L Authentics (PET)",
"ProductionCounts": 457615
}, {
"Part": "1.0 L Margarita Mix / PET",
"ProductionCounts": 660982
}, {
"Part": "other",
"ProductionCounts": 1571985
}]
}, ],
"dateArray": ["2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01"],
"xAxis": "Part",
"yAxis": "ProductionCounts",
"interestingMoments": []
}
请参见此处:尝试以下操作:
console.log( jsondata.xAxis );
试试这个:
console.log( jsondata.xAxis );
jsonData.data[0].data[0][jsonData.xAxis]
和jsonData.data[0].data[0][jsonData.yAxis]
jsonData.data[0]。data[0][jsonData.xAxis]
和jsonData.data[0]。data[0][jsonData.yAxis]
您应该像这样使用json
var data={
"data": [{
"data": [{
"Part": "1.75 L ICON (Glass)",
"ProductionCounts": 1012620
}, {
"Part": "1.75 L Marg Mix (PET)",
"ProductionCounts": 1003278
}, {
"Part": "1.75 L Authentics (PET)",
"ProductionCounts": 457615
}, {
"Part": "1.0 L Margarita Mix / PET",
"ProductionCounts": 660982
}, {
"Part": "other",
"ProductionCounts": 1571985
}]
}, ],
"dateArray": ["2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01"],
"xAxis": "Part",
"yAxis": "ProductionCounts",
"interestingMoments": []
};
console.log(data.xAxis);
console.log(data.yAxis);
您应该像这样使用json
var data={
"data": [{
"data": [{
"Part": "1.75 L ICON (Glass)",
"ProductionCounts": 1012620
}, {
"Part": "1.75 L Marg Mix (PET)",
"ProductionCounts": 1003278
}, {
"Part": "1.75 L Authentics (PET)",
"ProductionCounts": 457615
}, {
"Part": "1.0 L Margarita Mix / PET",
"ProductionCounts": 660982
}, {
"Part": "other",
"ProductionCounts": 1571985
}]
}, ],
"dateArray": ["2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01"],
"xAxis": "Part",
"yAxis": "ProductionCounts",
"interestingMoments": []
};
console.log(data.xAxis);
console.log(data.yAxis);
我会从冗余的“数据”对象中展开它 之后看起来更干净,效果也不错。您的JSON对象也未能通过JSLint。在这里发布之前,请始终通过JSLint或类似产品运行代码
var a = {
"data": [{
"data": [{
"Part": "1.75 L ICON (Glass)",
"ProductionCounts": 1012620
}, {
"Part": "1.75 L Marg Mix (PET)",
"ProductionCounts": 1003278
}, {
"Part": "1.75 L Authentics (PET)",
"ProductionCounts": 457615
}, {
"Part": "1.0 L Margarita Mix / PET",
"ProductionCounts": 660982
}, {
"Part": "other",
"ProductionCounts": 1571985
}]
}, ],
"dateArray": ["2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01"],
"xAxis": "Part",
"yAxis": "ProductionCounts",
"interestingMoments": []
};
a.data[0].data[0][a.xAxis] // for access part values
a.data[0].data[0][a.yAxis] // for acsess ProductionCounts values
我会从冗余的“数据”对象中展开它 之后看起来更干净,效果也不错。您的JSON对象也未能通过JSLint。在这里发布之前,请始终通过JSLint或类似产品运行代码
var a = {
"data": [{
"data": [{
"Part": "1.75 L ICON (Glass)",
"ProductionCounts": 1012620
}, {
"Part": "1.75 L Marg Mix (PET)",
"ProductionCounts": 1003278
}, {
"Part": "1.75 L Authentics (PET)",
"ProductionCounts": 457615
}, {
"Part": "1.0 L Margarita Mix / PET",
"ProductionCounts": 660982
}, {
"Part": "other",
"ProductionCounts": 1571985
}]
}, ],
"dateArray": ["2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01"],
"xAxis": "Part",
"yAxis": "ProductionCounts",
"interestingMoments": []
};
a.data[0].data[0][a.xAxis] // for access part values
a.data[0].data[0][a.yAxis] // for acsess ProductionCounts values