在html表中显示一个json数组

时间:2018-06-06 20:05:58

标签: javascript html

我正在尝试在HTML中显示一个JSON数组,我得到的响应是

[
  {
    "New  upto 3 Yrs": 40.0,
    "Above 3 yr to 9 yrs": 35.0,
    "Above 9 yrs upto 12 yrs": 30.0
  }
]

这里我的价值未定义。 (见快照output image) 如何在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");
                } 
            }}

1 个答案:

答案 0 :(得分:1)

您的属性名称中有额外的空格,如果您更改了该属性,则可以访问这些属性:

您的代码:

&#13;
&#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"]); // undefined
//                                         ^
&#13;
&#13;
&#13;

正确的间距:

&#13;
&#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;
&#13;
&#13;