我的JSON是這樣的:如何循環播放此JSON並將內容附加到div中的html?
{
"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"
}
]
}
可有人指南中告訴我,我怎麼遍歷這個JSON並將其追加到HTML。 我正在嘗試這樣的事情?
$(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
});
});
我呈現的頁面應該是這個樣子?
<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>
任何幫助深表謝意。
邁克
我越來越[object object] aler t在這種情況下 – Mike
這是正確的行爲。您創建DOM元素(div),這是一個對象。嘗試傳遞'it.title'以提醒更詳細的輸出。或者使用瀏覽器的'console.log'和JavaScript控制檯檢查創建的div的內容。 –