简体   繁体   English

在页面中显示json结果

[英]Display json result in page

I have the following js script and html codes and would like to output the result on a page. 我有以下js脚本和html代码,并想在页面上输出结果。 When I run it in console I can see the results as follows: 当我在控制台中运行它时,我可以看到如下结果:

[Object { title="They didn't Bow; They didn't Burn", link="http://xxxxxxx.com/bow-burn/", author="Admin", more...}, Object { title="When last did you antic...ant areas of your life?", link="http://xxxxxxx...significant-areas-life/", author="Admin", more...}, Object { title="Marriage Is Honourable In All…", link="http://xxxxxxx...arriage-honourable-all/", author="Admin", more...}, Object { title="Dwelling In The Strongholds Of God", link="http://xxxxxxx...elling-strongholds- [对象{title =“他们没有鞠躬;他们没有燃烧”,link =“ http://xxxxxxx.com/bow-burn/”,author =“ Admin”,更多...},对象{ title =“您最后一次滑稽的生活吗?”,链接=“ http://xxxxxxx...significant-areas-life/”,author =“ Admin”,更多...},对象{title =“婚姻在所有方面都是光荣的…”,link =“ http://xxxxxxx...arriage-honourable-all/”,author =“ Admin”,更多...},对象{title =“住宅在“上帝的堡垒”中,链接=“ http://xxxxxxx...elling-strongholds-

html code: html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">

    <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
    Remove this if you use the .htaccess -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <script src="css/style.css"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
    <script src="js/script.js"></script>
</head>
<body>

    <div id="blog" data-role="page">
    <div data-role="header" class="sys_hd" data-position="fixed" data-id="sys_header" >
        <h1>Sysads Posts</h1>
        </div><!-- header -->
        <div data-theme="c" data-role="content" id="postlist"> </div><!-- content -->
        <div data-role="footer" data-position="fixed" data-id="sys_footer" >
                    <div data-role="navbar" >
                <ul>
                    <li><a href="#blog" class="sys_ft">Home</a></li>
                    <li><a href="#blog" class="sys_ft">Disclaimer</a></li>
                </ul>
            </div><!-- navbar --> 
        </div><!-- footer --> 
    </div><!-- page -->
</body>
</html>

js code: js代码:

$(document).ready((function(){
     url = 'http://xxxxxxxx.com/category/daily-devotion/feed/';
        $.ajax({
        type: "GET",
        url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
        dataType: 'json',
        error: function(){
            alert('Unable to load feed, Incorrect path or invalid feed');
        },
        success: function(xml){
            postlist = xml.responseData.feed.entries;
            console.log(postlist);
        }
    });
}));

It may be malformed json. json格式可能不正确。 What does http://json.parser.online.fr/ say? http://json.parser.online.fr/说什么?

试试stringfy方法:

JSON.stringfy(obj)

try : 尝试:

for(var i in postlist) {
   var obj = postlist[i];
   console.log("title >>>  "+obj.title);
   console.log("link>>>  "+obj.link);
   ...........//other properties
}

It isn't entirely clear from your question what you are trying to accomplish, but from the title and the provided code, I'm guessing you want to add the retrieved entries to the '#postlist' part of your page. 从您的问题中尚不清楚您要完成什么,但是从标题和提供的代码来看,我猜您想将检索到的条目添加到页面的“ #postlist”部分。 If that is right, you could do something like this: 如果正确,则可以执行以下操作:

success: function(xml){
    postlist = xml.responseData.feed.entries;

    $.each(postlist, function(entry){

         $('#postlist').append($('<div class="entry">' + entry.title + '</div>'));

    });    

}

And obviously add whatever else you like markup-wise. 显然,添加其他您喜欢的标记方式。

Hope that helps. 希望能有所帮助。

Try this: 尝试这个:

success: function(data ){
            var postlist = data.responseData.feed.entries;
            var html = '<ul data-role="listview" data-filter="true">' ;
            for (var i = 0 ; i < 10 ; i++) {
                var entry = postlist[i];
                html += '<li>';
                html += '<a href="' + entry.link + '">' ;
                html += '<div>' + entry.title + '</div>' ;
                html += '<div>' + entry.author + '</div>' ;
                html += '</a>';
                html += '</li>';
            }
            html += '</ul>';
           $( "#postlist" ).append(html);

        }});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM