简体   繁体   English

如何遍历此JSON并将内容附加到div中的html?

[英]How do I loop over this JSON and append the content to html in a div?

My JSON looks like this: 我的JSON看起来像这样:

{
      "content": [
        {
          "title": "'I need some rest'",
          "description": "Descript One",
          "link": "http://www.google.com/?id=90#hhjjhjC-RSS",
          "guid": "http://www.google.com/?id=90",
          "pubDate": "Fri, 15 Oct 2002 08:27:27 GMT"
        },
        {
          "title": "I have no objection",
          "description": "descroption will ne populated here",
          "link": "http://www.google.com/?id=90#hhjjhjC-RSSRSS",
          "guid": "http://www.google.com/?id=9werwer0",
          "pubDate": "Fri, 14 Aug 2009 15:54:26 GMT"
        },
        {
          "title": "Mised the Buss",
          "description": "Missed the bus decsription",
          "link": "http://www.google.com/?CMP=OTC-RSS",
          "guid": "http://www.google.com/",
          "pubDate": "Fri, 21 Jul 2009 15:08:26 GMT"
        }



      ]
    }

Can someone guide in telling me how do I loop over this JSON and append it to html. 有人可以告诉我如何遍历此JSON并将其附加到html吗? I was trying something like this? 我正在尝试这样的事情?

$(function() { 
            $.getJSON("http://crossdomain.com/path/to/abovejsonfile", function(data) {
                $.each(data.item[0], function(index, it){
                     var d = $('<div>'+ it.title +'</div>');
                    alert(d);   //doesn't work          
                });
            });

My rendered page should be like this? 我呈现的页面应该是这样的?

<div class="container">
<div class="node" id="n1">
<div class="title">Title should come here</div>
<a class="link">Link</a>
<div class='d'>Date should be here</div>
</div>

<div class="node" id="n2">
<div class="title">Title should come here</div>
<a class="link">Link</a>
<div class='d'>Date should be here</div>
</div>

<div class="node"  id="n3">
<div class="title">Title should come here</div>
<a class="link">Link</a>
<div class='d'>Date should be here</div>
</div>
......
</div>

Any help is deeply appreciated. 任何帮助深表感谢。

Mike 麦克风

您需要遍历data["content"]数组的元素:

$.each(data["content"], function(index, it){

Save your self the hassle of doing this manually using loops and use a client side templating engine like json2html.com or pure 使用循环并使用客户端模板引擎(如json2html.compure) ,省去自己手动执行此操作的麻烦

Here is what you're looking for using json2html 这是您使用json2html寻找的东西

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src='http://json2html.com/js/jquery.json2html-3.1-min.js'></script>

<div id='container'></div>

<script>

var data = 
{
  "content": [
    {
      "title": "'I need some rest'",
      "description": "Descript One",
      "link": "http://www.google.com/?id=90#hhjjhjC-RSS",
      "guid": "http://www.google.com/?id=90",
      "pubDate": "Fri, 15 Oct 2002 08:27:27 GMT"
    },
    {
      "title": "I have no objection",
      "description": "descroption will ne populated here",
      "link": "http://www.google.com/?id=90#hhjjhjC-RSSRSS",
      "guid": "http://www.google.com/?id=9werwer0",
      "pubDate": "Fri, 14 Aug 2009 15:54:26 GMT"
    },
    {
      "title": "Mised the Buss",
      "description": "Missed the bus decsription",
      "link": "http://www.google.com/?CMP=OTC-RSS",
      "guid": "http://www.google.com/",
      "pubDate": "Fri, 21 Jul 2009 15:08:26 GMT"
    }
  ]
};

var template = {"tag":"div","id":function(obj,index) {return('n'+index);},"class":"node","children":[
{"tag":"div","class":"title","html":"${title}"},
{"tag":"a","class":"link","href":"${link}","html":"${link}"},
{"tag":"div","class":"d","html":"${pubDate}"}
]};

$('#container').json2html(data.content,template);

</script>

You got the each method right, but in the each you tell him to iterate over the item with the index 0 您正确地使用了每个方法,但是在每个方法中,您告诉他对索引为0的项目进行迭代

$.each(data.item[0], ....

Now jQuery can iterate over objects too (http://docs.jquery.com/Utilities/jQuery.each ) but that's not what you want right now. 现在jQuery也可以遍历对象(http://docs.jquery.com/Utilities/jQuery.each),但这不是您现在想要的。

If you want to iterate over an array, with each you have to passs an array (like in any other language which supports each/foreach)! 如果要遍历数组,则每个数组都必须传递一个数组(就像支持每个/ foreach的任何其他语言一样)!

So just use 所以就用

$.each(data.item[0], ....

and your code works fine. 并且您的代码可以正常工作。

how do i append links/divs with ids to html: 如何将具有ID的链接/ div附加到html:

$('<p>Hello World</p>').appendTo('body');  

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

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