-1

我尝试使用博主 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>');
    }
}
4

1 回答 1

0

使用 for 循环遍历类别项目并获取所有帖子标签

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>');
    }
}
于 2018-10-07T06:49:04.553 回答