如何加载具有对象对象的JSON文件的每个部分? [英] How do I load every section of a JSON file that has objects of objects?

查看:77
本文介绍了如何加载具有对象对象的JSON文件的每个部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON文件如下所示:

My JSON file looks like the following:

[{ "articles": [
    { "1": { "sections":
            [ {"1": "Lots of stuff here."} ]
        }
    },
    { "2": { "sections":
            [ {"1": "And some more text right here"} ]
        }
    }
}]

基本前提是,它在整个数组中只有一个元素称为"articles",它等于"sections"的数组.

The basic premise is that it has one element in the overall array called "articles", which is equal to an array of "sections".

换句话说,这是宪法,所以看起来像这样:

In other words, it's a constitution, so it looks like this:

第十条

-A部分

-B部分

-C部分

Z条

...

我希望能够提取每篇文章并将其加载到

I want to be able to pull out each article and load it into

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

这是我到目前为止拥有的脚本:

and this is the script I have so far:

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文件中的数据?

Basically, I just want to be able to reproduce a larger box (as shown in the <html> code above) for each article, with a list (<dl></dl>) for each section. How do I access the data from the JSON file correctly?

推荐答案

当您拥有一个数组,其中包含一个对象,该对象包含一个article属性,该属性是一个包含文章的数组,因此您需要深入到该属性以遍历他们.在回调函数中使用两个属性,以便您可以访问article对象:

As you have an array that contains an object that contains the article property that is an array that contains articles, you need to get down to that property to loop through them. Use two properties in the callback function so that you can access the article object:

$.each(d[0].articles, function(i, article) {

由于每个项目都是一个包含sections属性的对象,该属性是一个包含各节的数组,因此您需要深入到每个项目中的该属性.为各节使用其他索引变量,以便您仍可以访问用于文章索引的i变量:

As each item is an object that contains a sections property that is an array that contains the sections, you need to get down to that property in each item. Use a different index variable for the sections, so that you still can access the i variable used for the article index:

var mySections = article.sections;
$.each(mySections, function(j, section) {

在该循环内,j是该部分的索引,section是该部分的字符串,而i仍是该文章的索引.

Inside that loop, j is the index of the section, section is the section string, and i is still then index of the article.

这应该使您在循环中处于正确的轨道,其余的大部分只是为每个值使用正确的变量.

That should get you on the right track with the looping, the rest is mostly just using the right variable for each value.

这篇关于如何加载具有对象对象的JSON文件的每个部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆