javascript - 在 html 表中显示一个 json 数组
问题描述
我正在尝试在 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");
}
}}
解决方案
您的属性名称中有额外的空格,如果您更改它,您可以很好地访问属性:
你的代码:
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
// ^
使用正确的间距:
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
// ^^
推荐阅读
- fullcalendar - 全日历列表视图中的自定义列
- sql - Oracle SQL order by 不会对多列进行排序
- python - 将字符串转换为数据框中多列的浮点数
- lua - LUA - 计时器结束后无法让游戏重新开始
- docker - 如何将 docker run -it bash 命令转换为 docker-compose?
- r-markdown - 使用 RMarkdown 和 Flextable 在 Word 中生成具有“允许跨页分隔行”属性的表格
- java - “IllegalArgumentException:java.util.HashMap 类型的目标 bean 不是持久实体的类型”使用嵌入文档时
- javascript - 在 EventListener 函数中访问 HTML 元素的文本
- visual-studio - TypeScript:符号“日期”无法正确解析
- php - Laravel 5.5 某些未知情况下登录失败