I am new to JSON and am having difficulties with displaying data on a webpage using getJSON for a university assignment. I have tried a loop to append table rows but am a bit unclear how to use the data that is returned from the JSON file.
Here is what I tried to get working:
<body>
<table>
<tr>
<th>year</th>
<th>sem</th>
<th>module</th>
<th>crn</th>
<th>title</th>
<th>credits</th>
<th>level</th>
<th>coordinator</th>
</tr>
</table>
<script src="//code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$.getJSON('modules.json', function(data) {
console.log(data);
var tr;
for (var i = 0; i < data.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + data[i].year + "</td>");
tr.append("<td>" + data[i].sem + "</td>");
tr.append("<td>" + data[i].module + "</td>");
tr.append("<td>" + data[i].crn + "</td>");
tr.append("<td>" + data[i].title + "</td>");
tr.append("<td>" + data[i].credits + "</td>");
tr.append("<td>" + data[i].level + "</td>");
tr.append("<td>" + data[i].coordinator + "</td>");
$('table').append(tr);
}
});
</script>
</body>
When inspecting get response I can see that the JSON data has successfully been returned but despite trying to implement many examples I have not been able to display it in a table. My understanding is that it's along the lines of appending rows in a loop but I just can't seem to get any table output no matter what I try. My webpage right now just shows the table headers.
Here is a look at my json file:
{ "modules":[
{
"year": 2,
"sem": 1,
"module": "COM145",
"crn": 1865,
"title": "Content Authoring",
"credits": 10,
"level": 5,
"coordinator": "Clarke Kent"
},
{
"year": 1,
"sem": 3,
"module": "COM1333",
"crn": 2763,
"title": "Acad Study Skills for M'Media",
"credits": 10,
"level": 5,
"coordinator": "Bruce Wayne"
}
]}
Thanks