【问题标题】:how to retrieve location via json in Blogger如何在 Blogger 中通过 json 检索位置
【发布时间】:2018-04-17 04:36:15
【问题描述】:

我在 Blogger 中使用此脚本通过 JSON 显示带标签的帖子, 正如您通过这段代码看到的,我可以检索帖子的标题、缩略图和日期等

 <script type='text/javascript'>
    //<![CDATA[
    function labelthumbs(json) {
        document.write('<ul id="label_with_thumbs">');
        for (var i = 0; i < numposts; i++) {
            var entry = json.feed.entry[i];
            var posttitle = entry.title.$t;
            var posturl;
            if (i == json.feed.entry.length) break;
            for (var k = 0; k < entry.link.length; k++) {
                if (entry.link[k].rel == 'replies' && entry.link[k].type == 'text/html') {
                    var commenttext = entry.link[k].title;
                    var commenturl = entry.link[k].href;
                }
                if (entry.link[k].rel == 'alternate') {
                    posturl = entry.link[k].href;
                    break;
                }
            }
            var thumburl;
            try {
                thumburl = entry.media$thumbnail.url;
            } catch (error) {
                s = entry.content.$t;
                a = s.indexOf("<img");
                b = s.indexOf("src=\"", a);
                c = s.indexOf("\"", b + 5);
                d = s.substr(b + 5, c - b - 5);
                if ((a != -1) && (b != -1) && (c != -1) && (d != "")) {
                    thumburl = d;
                } else thumburl = 'https://1.bp.blogspot.com/-etyXYLaEXP8/WrmiznwRzXI/AAAAAAAAAnQ/Pu4OVqzx_4kmgwnmxJGDuf6j-l6ARuHbgCLcBGAs/s1600/nothumbnailimage.png';
            }
            var postdate = entry.published.$t;
            var cdyear = postdate.substring(0, 4);
            var cdmonth = postdate.substring(5, 7);
            var cdday = postdate.substring(8, 10);
            var monthnames = new Array();
            monthnames[1] = "January";
            monthnames[2] = "February";
            monthnames[3] = "March";
            monthnames[4] = "April";
            monthnames[5] = "May";
            monthnames[6] = "June";
            monthnames[7] = "July";
            monthnames[8] = "August";
            monthnames[9] = "September";
            monthnames[10] = "October";
            monthnames[11] = "November";
            monthnames[12] = "December";
            document.write('<li>');
            if (showpostthumbnails == true)
                document.write('<a class="posturl" href="' + posturl + '" target ="_top"><img class="label_thumb" src="' + thumburl + '"/></a>');
            document.write('<a class="posturl" href="' + posturl + '" target ="_blank"><h3 class="posttitle">' + posttitle + '</h3><span class="dateposted">' + monthnames[parseInt(cdmonth, 10)] + ' ' + cdday + ', ' + cdyear + '></span></a>');
            if ("content" in entry) {
                var postcontent = entry.content.$t;
            } else
            if ("summary" in entry) {
                var postcontent = entry.summary.$t;
            } else var postcontent = "";
            var re = /<\S[^>]*>/g;
            postcontent = postcontent.replace(re, "");
            if (showpostsummary == true) {
                if (postcontent.length < numchars) {
                    document.write('');
                    document.write(postcontent);
                    document.write('');
                } else {
                    document.write('');
                    postcontent = postcontent.substring(0, numchars);
                    var quoteEnd = postcontent.lastIndexOf(" ");
                    postcontent = postcontent.substring(0, quoteEnd);
                    document.write(postcontent + '...');
                    document.write('');
                }
            }
            var towrite = '';
            var flag = 0;
            if (showpostdate == true) {
                flag = 1;
            }
            if (showcommentnum == true) {
                if (flag == 1) {
                    towrite = towrite + ' | ';
                }
                if (commenttext == '1 Comments') commenttext = '1 Comment';
                if (commenttext == '0 Comments') commenttext = 'No Comments';
                commenttext = '<a href="' + commenturl + '" target ="_top">' + commenttext + '</a>';
                towrite = towrite + commenttext;
                flag = 1;;
            }
            if (displaymore == true) {
                if (flag == 1) flag = 1;
            }
            document.write(towrite);
            document.write('</li>');
            if (displayseparator == true)
                if (i != (numposts - 1))
                    document.write('');
        }
        document.write('</ul>');
    }
    //]]>
</script>

我的问题是,在我博客中的每篇文章中,我都可以选择指定 位置名称,例如:美国等。

那么我怎样才能通过 JSON 检索位置呢?

P.S这是出现在我的博客模板中的位置名称的 HTML

<span class='published-location' expr:title='data:post.location.name'><data:post.location.name/></span>

【问题讨论】:

    标签: javascript post blogs blogger


    【解决方案1】:

    通过帖子编辑器输入的位置数据可通过键 georss$featurename 在 JSON 提要中获得。您必须将其包含在您的代码中

    ..
    var entry = json.feed.entry[i];
    var posttitle = entry.title.$t;
    var location = entry.georss$featurename.$t;
    var posturl;
    ..

    然后通过上述代码中的document.write 语句将其打印到页面上-

    document.write('Location: ' + location + '<br/><a class="posturl" href="' + posturl + '" target ="_blank"><h3 class="posttitle">' + posttitle + '</h3><span class="dateposted">' + monthnames[parseInt(cdmonth, 10)] + ' ' + cdday + ', ' + cdyear + '></span></a>');
    

    【讨论】:

    • 你知道吗?我通常在博客中关注你的创造性答案,今天我希望你能回答我,你做到了!它有效!
    • 所以通过 json 可以检索帖子中具有特定类/id 的任何其他元素,不是吗?我知道我可以使用 (entry.content.$t).. 来检索整个内容。但是如果我想检索例如

      Hello World

    • 您将首先通过 entry.content.$t 检索帖子的完整 HTML 内容,然后对其进行解析以从中提取特定元素 - 请参阅 stackoverflow.com/questions/494143/…stackoverflow.com/questions/3103962/…
    • 不幸的是,我没有找到任何准确的答案来解释如何从 entry.content.$t 中检索具有 ID 的元素:/ 你很聪明,我相信你可以将它简化给我,谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多