0

I have a json data and i have a html table, i want to add the json data to table this is how i try to get the json data , i use append to add the table data, since i am new to json parsing i tried my best can any one help ...

I have a json data and i have a html table, i want to add the json data to table this is how i try to get the json data , i use append to add the table data, since i am new to json parsing i tried my best can any one help ...

<!DOCTYPE html>
<html>

<head>
    <title>JSON Demo</title>
    <style>
        table,
        th,
        td {
            border: 1px solid black;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        function StudentDetails() {
            var stu = [{
                    "stuname": "anbu",
                    "studep": "cs"
                },
                {
                    "stuname": "raj",
                    "studep": "Maths"
                },
                {
                    "stuname": "mani",
                    "studep": "science"
                }
            ]

            var tr = "";
            for (var i = 0; i < stu.length; i++) {

                tr = $('<tr/>');
                tr.append("<td>" + stu.stuname + "</td>");
                tr.append("<td>" + stu.studep + "</td>");
                $('#student').append(tr);

            }
        }
    </script>
</head>

<body>
    <input type="button" onClick="StudentDetails()" value="Student Table" />
    <div>
        <table id="student">
            <thead>
                <tr>
                    <th>Student Name</th>
                    <th>Student Department</th>
                </tr>
                <thead>
                    <tbody></tbody>
        </table>
    </div>

    <div id="showData"></div>
</body>

</html>
0

3 Answers 3

1

Add the index of Array to stu

tr.append("<td>" + stu[i].stuname + "</td>");
tr.append("<td>" + stu[i].studep + "</td>");
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

tr.append("<td>" + stu[i]['stuname'] + "</td>");

Comments

1

Change your code like this

for (var i = 0; i < stu.length; i++) {
        var tr = $("<tr>");

        tr.append("<td>" + stu[i].stuname + "</td>" + "<td>" + stu[i].studep + "</td>")

        $("#student tbody").append(tr);
      }
    }

<!DOCTYPE html>
<html>

<head>

  <title>JSON Demo</title>
  <style>
    table,
    th,
    td {
      border: 1px solid black;
    }
  </style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
  <input type="button" onClick="StudentDetails()" value="Student Table" />
  <div>
    <table id="student">
      <thead>
        <tr>
          <th>Student Name</th>
          <th>Student Department</th>
        </tr>
        <thead>
          <tbody></tbody>
    </table>
  </div>


  <div id="showData"></div>
  <script>
    function StudentDetails() {
      var stu = [{
          "stuname": "anbu",
          "studep": "cs"
        },
        {
          "stuname": "raj",
          "studep": "Maths"
        },
        {
          "stuname": "mani",
          "studep": "science"
        }
      ]



      for (var i = 0; i < stu.length; i++) {
        var tr = $("<tr>");

        tr.append("<td>" + stu[i].stuname + "</td>" + "<td>" + stu[i].studep + "</td>")

        $("#student tbody").append(tr);
      }
    }
  </script>

</body>


</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.