【发布时间】:2014-11-24 15:03:38
【问题描述】:
我的 JSON 文件如下所示:
[{ "articles": [
{ "1": { "sections":
[ {"1": "Lots of stuff here."} ]
}
},
{ "2": { "sections":
[ {"1": "And some more text right here"} ]
}
}
}]
基本前提是它在整个数组中有一个元素叫做"articles",等于"sections"的数组。
换句话说,它是一部宪法,所以它看起来像这样:
第十条
-A部分
-B节
-C部分
第 Z 条
...
我希望能够拉出每篇文章并将其加载到
<div class="alert text-black" id="article">
<p class="lead" id="articleHeader"><strong>Article 1</strong></p>
<dl>
<dt id="article_section">Section 1</dt>
<dd id="article_sectionINFO">this stuff belongs to section 1</dd>
</dl>
</div>
这是我目前的脚本:
var loadEvents = function(d){
$.each(d, function(i){
var articleN = i;
var articleID = "article" + i;
var articleHeaderID = "articleHeader" + i;
var articleHeader = "<p class='lead' id='articleHeader'><strong>Article " + i + "</strong></p>";
var mySections = new Array();
$.each(d, function(i){
var sectionN = i;
var sectionID = "article" + articleN + "section" + sectionN;
var sectionINFO = sectionID + "INFO";
mySections[k] =
"<dt id='" + sectionID +"'> Section " + sectionN + "</dt>" +
"<dd id='" + sectionINFO +"'>" + d[sectionN].
})
$("#eventHolder").append(new_block(articleID, articleHeaderID, articleHeader)); //to call the new element
})}
$.getJSON('data/constitution.json', loadConstitution);
基本上,我只是希望能够为每篇文章复制一个更大的框(如上面的<html> 代码所示),每个部分都有一个列表(<dl></dl>)。如何正确访问 JSON 文件中的数据?
【问题讨论】:
-
一个不完整的问题如何获得投票?
-
@starskythehutch 我猜有些人只是瞥了一眼,看到那里发布的代码然后去“是的!这很好!” :(
-
由于某种原因,它切断了我问题的最后一部分。我修好了。
标签: javascript jquery html json