简体   繁体   English

JSON循环遍历嵌套数组

[英]JSON Loop through nested array

This is my JS ("data" is from the json call): 这是我的JS(“数据”来自json调用):

if (data.projectReports.length) {
  for (var i in data.projectReports){
    var report = data.projectReports[i];
    $('#reports').append(
        '<div class="report-description">' +
          '<h2>' + report.header + '</h2>' +
          '<p>' + report.text + '</p>' +
        '</div>' +
        '<ul class=\"report-moreinfo\">' +

          // I want to loop through "persons" here.

        '</ul>'
    );
  }
} else

. . .

This is my JSON: 这是我的JSON:

{
  "projectReports":[
    {
      "header":"Headline",
      "text":"Description of item",
      "estimate":10,
      "actual":7,
      "persons":{
        "Robert":5,
        "Erik":10,
        "Juan":3
      }
    }
  ]
}

I am new to JSON and after searching for an answer and actually finding a few different answers, new problems arose so I thought I would post the question in it's entirety here. 我是JSON的新手,在寻找答案并找到一些不同答案之后,出现了新问题,所以我想我将问题完整地发布在这里。

Where the comment is in my JavaScript code, I want to loop through report.persons . 我的JavaScript代码中的注释所在的位置,我想遍历report.persons

In all of the solutions I found they could point directly to "header" or "text" like I have done before, but in this case I just have a key and value. 在所有解决方案中,我发现它们可以像以前一样直接指向“标题”或“文本”,但是在这种情况下,我只有一个键和值。 How would I go about doing something like this? 我将如何做这样的事情?

<li><p> persons.key </p><p> persons.value </p></li>

I understand that I will have to do another for loop, but my knowledge isn't good enough to be able to figure out on my own how to construct it. 我知道我将不得不再做一次for循环,但是我的知识还不足以自己弄清楚如何构造它。

This is pretty basic stuff 这是很基本的东西

var personsMarkup = "";
for (var i in persons){
   if (persons.hasOwnProperty(i)){
     console.log(i);          // the key
     console.log(persons[i]); // the value
     // cancat this all together
     personsMarkup =+ "<li><p>"+i+"</p><p>"+persons[i]+"</p></li>";
   }
}

and then: 接着:

$('#reports').append(
    /* ... */
    '<ul class=\"report-moreinfo\">' +
    personsMarkup +
    '</ul>';
    /* ... */
);

Use for (.. in ..) for (.. in ..)

$('#reports').append(
    '<div class="report-description">' +
      '<h2>' + report.header + '</h2>' +
      '<p>' + report.text + '</p>' +
    '</div>' +
    '<ul class=\"report-moreinfo\">');

for (var personKey in report.persons){
  $('#reports').append('<li><p>' + personKey + '</p><p>' + report.persons[personKey] + '</p></li>');
}

$('#reports').append(
    '</ul>'
);

For your code I'd use a function that loops through reports.persons and returns what you need: 对于您的代码,我将使用一个遍历reports.persons并返回所需内容的函数:

var showPersons = function(persons){
  var appendedData = '';
  for (var person in persons) {
    if (!persons.hasOwnProperty(person)) continue;
    appendedData += '<li><p>' + person + '</p><p>' + persons[person] +'</p></li>'
  }
  return appendedData;
};

And now you can use this to append all that stuff inside the <ul> tags: 现在,您可以使用它在<ul>标记内附加所有内容:

listPersons(report.persons);

If you wanted the code read closer to what you wanted (and to be able to use person.name and person.value ), you'd need to have the JSON in this format: 如果您希望代码更接近您想要的内容(并能够使用person.nameperson.value ),则需要使用以下格式的JSON:

{
    "projectReports": [
        {
            "header": "Headline",
            "text": "Description of item",
            "estimate": 10,
            "actual": 7,
            "persons": [
                {
                    "name": "Robert",
                    "value": 5
                },
                {
                    "name": "Erik",
                    "value": 10
                },
                {
                    "name": "Juan",
                    "value": 3
                }
            ]
        }
    ]
}

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

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