我的JavaScript for循环遇到问题

时间:2015-11-23 07:00:40

标签: javascript php json for-loop

我试图遍历我返回的JSON数组并在表中显示值。我的数组包含多个具有多个值的对象,因此每个对象将=一个新行,每个值将=一个新单元格。

这是我尝试过的一个版本:

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);

};

我也尝试了以下内容:

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);

};

两个版本都返回相同的结果。我得到了表头,我在表中得到了一堆空行。我在循环中添加的调试计数在停止之前命中405。据我所知,我认为循环对于我的JSON结果的每个字符循环一次

关于我做得不对的任何想法? 感谢。

JSON响应(已经过测试,IS格式正确的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\/"}]

3 个答案:

答案 0 :(得分:2)

您的问题在于JSON解析。

首先执行此操作:这会将您的JSON字符串对象转换为Javascript对象,您可以在其中循环。

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\/"}]');

然后,这样做

for(i=0; i<items.length; i++){
    console.log(items[i].url);
    console.log(items[i].name); //etc
}

答案 1 :(得分:1)

将您的脚本更改为:

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);
}

答案 2 :(得分:0)

您需要解析JSON字符串。使用JSON.parse解析JSON后,如下所示

var items = JSON.parse(json_response);

您正在添加字符串,但未将其分配给$table

替换循环内的行,如下所示

$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>";