0

I am trying to display a JSON array in HTML which I get as response is

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

here i am getting value undefined. (see snap shot output image) How can I display this kind of JSON in HTML as a html table?

here is my script:

 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
  • 2
    looks like extra spaces (in keys) issue to me Commented Jun 6, 2018 at 20:10

1 Answer 1

1

You have extra spaces in your property names, you can access the properties just fine if you change that:

Your code:

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
//                                         ^

With correct spacing:

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
//                                         ^^

Sign up to request clarification or add additional context in comments.

3 Comments

with weird keys like this, I would rather go with Object.keys and read based on each key.
Yes, great idea!
thanx for the solution, I tried this but not getting value in HtML table, i need to bind this value into HTML Table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.