我正在尝试在HTML中显示一个JSON数组,我得到的响应是
[
{
"New upto 3 Yrs": 40.0,
"Above 3 yr to 9 yrs": 35.0,
"Above 9 yrs upto 12 yrs": 30.0
}
]
这里我的价值未定义。 (见快照) 如何在HTML中将这种JSON显示为html表?
这是我的剧本:
success: function (data) {
if (objMotorUnderwritingList.ddlVehicleType == "TW$") {
// alert("hi TW$");
if (data.jsDiscoundGridList != 0) {
var eachrow = "<tr>"
+ "<td>" + data.jsDiscoundGridList[0]["New upto 3 Yrs"] + "</td>"
+ "<td>" + data.jsDiscoundGridList[0]["Above 3 yr to 9 yrs"] + "</td>"
+ "<td>" + data.jsDiscoundGridList[0]["Above 9 yrs upto 12 yrs"] + "</td>"
+ "</tr>";
$('.tbody').html(eachrow);
}
else {
ShowAlert("Something Wrong in TW");
}
}}
答案 0 :(得分:1)
您的属性名称中有额外的空格,如果您更改了该属性,则可以访问这些属性:
您的代码:
let data = {
jsDiscoundGridList: [{
"New upto 3 Yrs": 40.0,
"Above 3 yr to 9 yrs": 35.0,
"Above 9 yrs upto 12 yrs": 30.0
}]
};
console.log(data.jsDiscoundGridList[0]["New upto 3 Yrs"]); // undefined
// ^
&#13;
正确的间距:
let data = {
jsDiscoundGridList: [{
"New upto 3 Yrs": 40.0,
"Above 3 yr to 9 yrs": 35.0,
"Above 9 yrs upto 12 yrs": 30.0
}]
};
console.log(data.jsDiscoundGridList[0]["New upto 3 Yrs"]); // 40
// ^^
&#13;