I am attempting to loop through my returned JSON array and display the values in a table. My array contains multiple objects with multiple values, so each object would = a new row and each value would = a new cell.
Here's one version I have tried:
function refreshUrlArray(urlsRetrieved) {
$('#response').html('Attempting to update your URLs, please wait...');
var urlsObj = urlsRetrieved;
console.log("JSON results returned from DB query: " + urlsObj);
//Create object holding table
$table = "<table id='urlTable'><td>Name</td><td>Release Time</td><td>Release Date</td><td>Category</td><td>Genre</td><td>URL</td>";
//debugging (loop count)
var c = 0;
for (var key in urlsObj) {
if (urlsObj.hasOwnProperty(key)) {
var val = urlsObj[key];
$table += "<tr>";
$table + "<td>" + val.name; + "</td>";
$table + "<td>" + val.releaseTime; + "</td>";
$table + "<td>" + val.releaseDate; + "</td>";
$table + "<td>" + val.category; + "</td>";
$table + "<td>" + val.genre; + "</td>";
$table + "<td>" + val.url; + "</td>";
$table += "</tr>";
//debugging (loop count)
c++;
console.log("Count: " + c);
}
}
$('#response').html('Attempting to display your URLs, please wait...');
$('#content01').append($table);
};
I also tried the following:
function refreshUrlArray(urlsRetrieved) {
$('#response').html('Attempting to update your URLs, please wait...');
var urlsObj = urlsRetrieved;
console.log("JSON results returned from DB query: " + urlsObj);
//Create object holding table
$table = "<table id='urlTable'><td>Name</td><td>Release Time</td><td>Release Date</td><td>Category</td><td>Genre</td><td>URL</td>";
//debugging (loop count)
var c = 0;
for (var i = 0; i < urlsObj.length; i++) {
$table += "<tr>";
$table + "<td>" + urlsObj[i].name; + "</td>";
$table + "<td>" + urlsObj[i].releaseTime; + "</td>";
$table + "<td>" + urlsObj[i].releaseDate; + "</td>";
$table + "<td>" + urlsObj[i].category; + "</td>";
$table + "<td>" + urlsObj[i].genre; + "</td>";
$table + "<td>" + urlsObj[i].url; + "</td>";
$table += "</tr>";
//debugging (loop count)
c++;
console.log("Count: " + c);
}
$('#response').html('Attempting to display your URLs, please wait...');
$('#content01').append($table);
};
Both versions are returning the same results. I get the table headers, and I get a bunch of empty rows in my table. The debugging count I added in the loop hits 405 before it stops. As far as I can guess, I think the loop is looping through one time for every individual character of my JSON results
Any ideas on what I am not doing correctly? Thanks.
JSON Response (has been tested an IS correctly formatted JSON):
[{"userId":4,"name":"Doctor Who","releaseTime":"2015-11-22 12:45:24","releaseDate":"Monday","category":"Television","genre":"Action","url":"http:\/\/www.netflix.com\/browse?jbv=70142441&jbp=0&jbr=1"},{"userId":4,"name":"Game Of Thrones","releaseTime":"2015-11-22 13:34:06","releaseDate":"Tuesday","category":"Television","genre":"Drama","url":"http:\/\/www.tvmuse.com\/tv-shows\/Game-of-Thrones_25243\/"}]