I'm trying to loop through JSON objects with jQuery in an AJAX call and then print the objects in html page. I came across this stackoverflow post that shows you how to loop through json objects. Which worked to a certain point.
I am able to display all the values from object with id": 1, but I'm having trouble trying to display more values after that. So for example I would like to display the values that are stored in "id": 2 and so on.
The JSON data is generated by https://openhardwaremonitor.org software.
Here's how the JSON data looks like in short version , and here's the full version
{
"id": 0,
"Text": "Sensor",
"Min": "Min",
"Value": "Value",
"Max": "Max",
"ImageURL": "",
"Children": [
{
"id": 1,
"Text": "LAPTOP-4CG0QVS4",
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/computer.png",
"Children": [
{
"id": 2,
"Text": "ASUS FX504GM",
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/mainboard.png",
"Children": []
},
{
"id": 3,
"Text": "Intel Core i7-8750H",
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/cpu.png",
"Children": [
{
"id": 4,
"Text": "Clocks",
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/clock.png",
"Children": [
{
Here's the code that I currently have
<script>
var url = 'https://api.myjson.com/bins/sa41o';
$.ajax({
url: url,
method: 'GET',
}).done(function (result) {
var data = result.Children;
//console.log(result.Children.length);
var i = 0;
var hosData = "<table border='1'>";
hosData += "<tr>";
hosData += "<th>";
hosData += 'id';
hosData += "</th>";
hosData += "<th>";
hosData += 'Text';
hosData += "</th>";
hosData += "<th>";
hosData += 'Min';
hosData += "</th>";
hosData += "<th>";
hosData += 'Value';
hosData += "</th>";
hosData += "<th>";
hosData += 'Max';
hosData += "</th>";
hosData += "<th>";
hosData += 'ImageURL';
hosData += "</th>";
hosData += "</tr>";
for (i = 0; i < data.length; i++) {
hosData += "<tr>";
hosData += "<td>";
hosData += data[i].id;
hosData += "</td>";
hosData += "<td>";
hosData += data[i].Text;
hosData += "</td>";
hosData += "<td>";
hosData += data[i].Min;
hosData += "</td>";
hosData += "<td>";
hosData += data[i].Value;
hosData += "</td>";
hosData += "<td>";
hosData += data[i].Max;
hosData += "</td>";
hosData += "<td>";
hosData += data[i].ImageURL;
hosData += "</td>";
hosData += "</tr>";
}
hosData += "</table>";
$("#data").html(hosData);
}).fail(function (err) {
throw err;
});
<script
.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="data">
</div>
I can't seem to wrap my head around this problem.
var data = result.Children
returns length as 1, which is the issue here as I'm expecting the length to be larger than 1.