首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将JSON数据解析为HTML

将JSON数据解析为HTML
EN

Stack Overflow用户
提问于 2014-04-07 02:04:14
回答 1查看 223关注 0票数 1

我正在使用Google来显示我Facebook上的最新帖子。

这些雕塑来自fbrss.com的一个XML文件,下面的代码成功地工作了:

代码语言:javascript
复制
    <script type="text/javascript">
    google.load("feeds", "1")

    $.ajax({
        url      : document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=3&callback=?&q=' + encodeURIComponent('feedhere.xml'),
        dataType : 'json',
        success  : function (data) {
            if (data.responseData.feed && data.responseData.feed.entries) {
                $.each(data.responseData.feed.entries, function (i, e) {
                    console.log("------------------------");
                    console.log("postContent     : " + e.title);
                    console.log("link    : " + e.link);
                    console.log("date:      " + e.publishedDate);

                });
                ;
            }
        }
    });
</script>

我从控制台得到的响应,就像预期的那样

代码语言:javascript
复制
    postContent     : Example post, just testing our facebook news right now!
link    : https://www.facebook.com/LINKTOPOST
date:      Fri, 04 Apr 2014 23:54:02
------------------------
postContent     : We're getting ready to launch our new website! Stay tuned for more updates and more info. We're very excited.
link    : https://www.facebook.com/LINKTOPOST
date:      Fri, 04 Apr 2014 23:06:59 -0700

现在,我尝试将jsonData附加到这样的div中,但我认为我做得不对。

代码语言:javascript
复制
 if (data.responseData.feed && data.responseData.feed.entries) {
                $.each(data.responseData.feed.entries, function (i, e) {
                    window.NewsPost = e.title;
                    console.log("------------------------");
                    console.log("postContent     : " + e.title);
                    console.log("link    : " + e.link);
                    console.log("date:      " + e.publishedDate);

                });
                                }

            $("#facebookrss").append("<div>Test" + NewsPost +"</div>");

        }
    });

我分配了一个全局变量,以便在它之外使用它。但我的脸书里什么都没有。

我正在尝试将日期和page内容分成单独的div,将postcontent包装在链接中,这样他们就可以访问facebook页面了。

我还试图将日期解析为"MM - DD“。Google似乎没有得到很好的充实,但我对javascript也非常陌生。

感谢任何能帮上忙的人。

编辑:已经成功解决了这个问题,谢谢卡克哈特。下面是我的最后一段适合格式化的代码,并包含更好的格式化日期。

代码语言:javascript
复制
$.ajax({
        url      : document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=3&callback=?&q=' + encodeURIComponent('feedhere.xml'),
        dataType : 'json',
        success  : function (data) {
            if (data.responseData.feed && data.responseData.feed.entries) {
                $.each(data.responseData.feed.entries, function (i, e) {
                    window.NewsPost = e.title;
                    console.log("------------------------");
                    console.log("postContent     : " + e.title);
                    console.log("link    : " + e.link);
                    console.log("date:      " + e.publishedDate);
                    // construct div tag to have the new post details

                    var date = new Date(e.publishedDate);
                    monthNames =
                        [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                            'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
                    datemonth = monthNames[ date.getMonth() ];
                    dateday = date.getDay();

                    var construct_news = '<div class="facebook-feed">';
                    construct_news += '<div class="left">';
                    construct_news += '<div class="footer-circle-large">';
                    construct_news +='<div class="month">' + datemonth + '</div>';
                    construct_news += '<div class="day">' + dateday + '</div>';
                    construct_news += '</div>';
                    construct_news += '</div>';
                    construct_news += '<div class="right">';

                    construct_news += '<div>';
                    construct_news += '<span class="facebook-update"><a target="_blank" href="'+ e.link+'">' + e.title + '</a></span>';

                    construct_news += '</div>';
                    construct_news += '</div>';
                    construct_news += '</div>';

                    $("#facebookrss").append(construct_news); // append it to rss div

                });
            }
        }
    });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-07 06:04:36

尝尝这个

代码语言:javascript
复制
if (data.responseData.feed && data.responseData.feed.entries) {
    $.each(data.responseData.feed.entries, function (i, e) {
        window.NewsPost = e.title;
        console.log("------------------------");
        console.log("postContent     : " + e.title);
        console.log("link    : " + e.link);
        console.log("date:      " + e.publishedDate);
        // construct div tag to have the new post details

        var new_post_tag = '<div>';
        new_post_tag += '<h4>' + e.title + '</h4>';
        new_post_tag += '<p>' + e.link + '</p>';
        new_post_tag += '<p>' + e.publishedDate + '</p>';
        new_post_tag += '</div>';

        $("#facebookrss").append(new_post_tag); // append it to rss div
    });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22902472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档