1

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\/"}]
t.niese
  • 39,256
  • 9
  • 74
  • 101
Corey
  • 327
  • 5
  • 15
  • *"As far as I can guess, I think the loop is looping through one time for every individual character of my JSON results"* In that case you haven't parsed the JSON into a JavaScript array. See [Parse JSON in JavaScript?](http://stackoverflow.com/q/4935632/218196) – Felix Kling Nov 23 '15 at 07:02
  • 1
    "*As far as I can guess, I think...*" - Why are you ***guessing***? Learn to use a debugger and ***know*** what your problem is. – Amit Nov 23 '15 at 07:04
  • And if you access your members using array accessor like val["name"], ...? – Rik Nov 23 '15 at 07:25
  • Not related to your problem, but a small note. You are for sure absolutely free to name the variables what ever you want, but in combination with jQuery I would use `$` prefixed variables only for variables that actually hold an jQuery object/result-set, and not for a plain html string. – t.niese Nov 23 '15 at 08:11
  • Thanks t.niese, i'll keep this in mind! – Corey Nov 24 '15 at 05:16

3 Answers3

2

Your problem is with JSON parsing.

First do this: This will convert your JSON string object to Javascript Objects, in which you can loop through.

var items = JSON.parse('[{"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\/"}]');

Then, do this

for(i=0; i<items.length; i++){
    console.log(items[i].url);
    console.log(items[i].name); //etc
}
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
1

Change your script to:

function refreshUrlArray(urlsRetrieved) {
    $('#response').html('Attempting to update your URLs, please wait...');  

    var urlsObj = urlsRetrieved;

    var details = JSON.parse(urlsObj);

    //console.log(details.length);

    table = "<table id='urlTable'><td>Name</td><td>Release Time</td><td>Release Date</td><td>Category</td><td>Genre</td><td>URL</td>";
    for(var i = 0; i < details.length; i++){
        records = details[i];
        //Create object holding table
        table += "<tr>";

        table += "<td>" + records.name; + "</td>";
        table += "<td>" + records.releaseTime; + "</td>";
        table += "<td>" + records.releaseDate; + "</td>";
        table += "<td>" + records.category; + "</td>";
        table += "<td>" + records.genre; + "</td>";
        table += "<td>" + records.url; + "</td>";

        table += "</tr>";

    }
    table += "</table>";

    $('#response').html('Attempting to display your URLs, please wait...');  
    $('#content01').html(table);
}
Apb
  • 979
  • 1
  • 8
  • 25
  • Thanks mate this helped, as most people pointed out I had not parsed the JSON to a JS array... Still learning guys, didn't realize my mistake. – Corey Nov 23 '15 at 07:27
0

You need to parse the JSON string. After parsing the JSON with JSON.parse as below,

var items = JSON.parse(json_response);

You are adding the strings but not assigning them to the $table.

Replace lines inside the loop as below

$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>";
Nijraj Gelani
  • 1,446
  • 18
  • 29