用ajax请求拉json数据

时间:2016-06-17 14:01:17

标签: javascript jquery json ajax

我正在尝试使用ajax提取一些json数据。我能够成功地从前两个项目中提取数据,但是我真正需要的“地标”下的数据。我需要能够像列出rssfeed一样在列表中显示它。我不确定如何深入研究json以获取我需要的数据。任何帮助非常感谢。

$.ajax({
    type: 'GET',
    url: rssAPI,
    success: function(data){
        console.log(data);
        $.each(data, function(index, item) {
            $.each(item, function(key, value) {
                $('.listing').append(key + ': ' + value + '</br>');
            });
        });
    }
});

这是我的json的样子 enter image description here

这是我的输出 enter image description here

2 个答案:

答案 0 :(得分:0)

如果属性值是字符串,则需要输出,或者如果它是一个数组,则需要进一步迭代(即另一个循环),就像Placemark属性一样。

所以:

    $.each(data, function(index, item) {
        $.each(item, function(key, value) {
            if (typeof value != 'object')
                $('.listing').append(key + ': ' + value + '</br>');
            else
                $.each(value, function(key, value) {
                    //these are your items under Placemark
                });
        });
    });

答案 1 :(得分:0)

这里地标包含json对象的数组。所以你需要按如下方式访问它:

for(i=0; i<Document.Placemark; i++){

    console.log(Document.Placemark[i].name);
    console.log(Document.Placemark[i].description);

}