繁体   English   中英

在html表中显示一个json数组

[英]display a json array in html table

我正在尝试在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 // ^^ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM