Blogger:使用JSON回调获取标签信息

时间:2018-10-07 05:56:18

标签: javascript blogger blogger-dynamic-views

我尝试使用Blogger json回调获取数据。使用以下Java脚本代码,我可以获得标题,链接,拇指,摘要和标签/类别。除标签/类别外,其他所有功能都很好。不幸的是,我只显示一个标签。如何循环通过博客帖子中的每个帖子标签?该帖子不仅具有一个标签,而且具有2或3个标签。

这是代码:

var numposts = 5;
function whatshot(json) {
    for (var i = 0; i < numposts; i++) {
        var entry = json.feed.entry[i];
        var posttitle = entry.title.$t;
        var category = entry.category[0].term;
        var summary = entry.summary.$t;
        var category;
        var posturl;
        var thumb = entry.media$thumbnail.url;
        var thumb = thumb.replace("s72-c", "s1600");
        if (i == json.feed.entry.length) break;
        for (var k = 0; k < entry.link.length; k++) {
            if (entry.link[k].rel == 'alternate') {
                posturl = entry.link[k].href;
                break;
            }           
        }               
        document.write('<ul class="collection top-highlight"><li class="collection-item avatar"><img class="circle" src="'+thumb+'"/><span class="title">Lipsum</span><p>'+category+'</p></li><li><p class="sp-status">'+summary+'</p></li><li><img class="responsive-img" src="'+thumb+'"/></li><li><h2 class="sp-title"><a href="'+posturl+'">'+posttitle+'</a></h2></li></ul>');
    }
}

1 个答案:

答案 0 :(得分:0)

用于循环浏览类别项目并获取所有帖子标签

for (var m = 0; m < entry.category.length; m++) {
    category += entry.category[m].term + " ";
}

每个标签名称后的注释空间

这是您的代码

var numposts = 5;
function whatshot(json) {
    for (var i = 0; i < numposts; i++) {
        var entry = json.feed.entry[i];
        var posttitle = entry.title.$t;
        var summary = entry.summary.$t;
        var category;
        for (var m = 0; m < entry.category.length; m++) {
            category += entry.category[m].term + " ";
        }
        var posturl;
        var thumb = entry.media$thumbnail.url;
        var thumb = thumb.replace("s72-c", "s1600");
        if (i == json.feed.entry.length) break;
        for (var k = 0; k < entry.link.length; k++) {
            if (entry.link[k].rel == 'alternate') {
                posturl = entry.link[k].href;
                break;
            }           
        }               
        document.write('<ul class="collection top-highlight"><li class="collection-item avatar"><img class="circle" src="'+thumb+'"/><span class="title">Lipsum</span><p>'+category+'</p></li><li><p class="sp-status">'+summary+'</p></li><li><img class="responsive-img" src="'+thumb+'"/></li><li><h2 class="sp-title"><a href="'+posturl+'">'+posttitle+'</a></h2></li></ul>');
    }
}