将JSON数据解析为HTML

时间:2014-04-07 02:04:15

标签: javascript jquery xml json rss

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

这些雕像来自fbrss.com的XML文件,我在下面的代码成功运作:

    <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>

我从控制台获得的响应,正如预期的那样

    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,但我认为我没有正确地做到这一点。

 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>");

        }
    });

我分配了一个全局变量,所以我可以在它之外使用它。但是我的#facebookrss div里面没有任何东西。

我正在尝试将日期和postcontent分成单独的div,postcontent包含在链接中,以便他们可以访问facebook页面。

我也试图将日期解析为“MM - DD”。 Google Feeds API似乎没有很好地充实,但我也是javascript的新手。

非常感谢能够提供帮助的任何人。

编辑:已经成功解决了,谢谢CACKHAROT。以下是我的最终代码,适用于格式化并包含更好的格式化日期。

$.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

                });
            }
        }
    });

1 个答案:

答案 0 :(得分:1)

试试这个

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
    });
}