如何获取Json对象数组值?

时间:2017-03-01 07:34:47

标签: jquery arrays json object

我正在尝试使用json和jquery显示帖子列表。

这就是我用来获取数据的原因

var app = {

    init: function() {
        app.getPosts();
    },

    getPosts: function() {

        var rootURL = 'https://www.example.com/wp-json/wp/v2/posts';

        $.ajax({
            type: 'GET',
            url: rootURL,
            dataType: 'json',
            success: function(data){

                $.each(data, function(index, value) {
                    console.log(value.featured_image);
                  $('ul.topcoat-list').append('<li class="topcoat-list__item">' +
                    '<h3>'+value.title+'</h3>' +
                    '<p>'+value.excerpt+'</p></li>');
                    ;
                });
            },
            error: function(error){
                console.log(error);
            }

        });

    }

}

但此代码只返回以下内容 output

但是当我去https://www.example.com/wp-json/wp/v2/posts时,我看到了完美的阵列。

那里有一个数组的例子

{
    "id":2267,
    "type":"post",
    "title":{"rendered":"When to visit Arugam Bay? &#8211; Arugam Bay Weather &#038; Seasons \u2013"},
    "content":{"rendered":"<div class=\"circle-headline\">\n<p>The right answer is<\/p>\n<\/div>\n<p class=\"wpk-circle-title text-custom\">ALL YEAR LONG!<\/p>\n<p>You can visit Arugam Bay anytime and always find amazing sunny weather. Despite this incredible where, there is a high and low season.<\/p>\n","protected":false},
    "excerpt":{"rendered":"<p>The right answer is<\/p>\n<p>ALL YEAR LONG!<br \/>\nYou can visit Arugam Bay anytime and always find amazing sunny weather. Despite this incredible where, there is a high and low season.<\/p>\n","protected":false},
    "author":1,
    "featured_media":2268,
    "comment_status":"open",
    "ping_status":"open",
    "sticky":false,
    "template":"",
    "format":"standard",
}

如何从那里获取标题和摘录并使用它显示列表?

1 个答案:

答案 0 :(得分:4)

访问包含实际字符串的呈现属性:

'<h3>'+value.title.rendered+'</h3>' +
'<p>'+value.excerpt.rendered+'</p></li>');
...