使用Javascript将深层嵌套的JSON输出到HTML

时间:2013-11-23 04:07:18

标签: javascript html json nested output

所以我可以通过转到console.log(data.response[0].entities.urls);来访问我需要的数据,但是如何将第二个实体数组输出为HTML格式。有没有办法使用for循环或其他东西来分解JSON数据?

$(document).ready(function () {


$.getJSON('http://www.jamesam.ac/dev202/sdillon3/', function (data) {

    var output = '<ul data-role="listview" data-filter="true">';

    $.each(data.response, function (key, val) {

            output += '<li>';
            output += '<img src="' + val.user.profile_image_url + '" alt="' + val.text + '" />';
            output += '<h3>' + val.user.name + '</h3>';
            output += '<h4><i>' + val.user.screen_name + '</i></h4>';
            output += '<p>' + val.text + '</p>';
            output += '</li>';

    }); // go through each post 
    output += '</ul>';


    $('#feed').html(output);

    console.log(data)
    //console.log(data.response[0].entities.urls);//

}); 

});

1 个答案:

答案 0 :(得分:0)

解决!

只需要写一个好的旧for循环。

$(document).ready(function () {


$.getJSON('http://www.jamesam.ac/dev202/sdillon3/', function (data) {

    var output = '<ul data-role="listview" data-filter="true">';

    $.each(data.response, function (key, val) {

            output += '<li>';
            output += '<img src="' + val.user.profile_image_url + '" alt="' + val.text + '" />';
            output += '<h3>' + val.user.name + '</h3>';
            output += '<h4><i>' + val.user.screen_name + '</i></h4>';
            output += '<p>' + val.text + '</p>';

            for(var i = 0; i < val.entities.urls.length; i++)
            {
                output+= '<a href="' + val.entities.urls[i].url +'" </a>';
            }
            output += '</li>';

    }); // go through each post 
    output += '</ul>';

    $('#feed').html(output);

}); 

});