简体   繁体   English

如何使用节点js遍历不规则的嵌套json

[英]How to iterate over an irregular nested json with node js

I'm making a little app at Nodejs and I'm struggling trying to loop an irregular JSON to print its data. 我在Nodejs上制作了一个小应用程序,但我正努力尝试循环一个不规则的JSON以打印其数据。

My JSON has the next structure: 我的JSON具有以下结构:

{
"courses": [
    {
        "java": [
            { "attendees": 43 },
            { "subject": "Crash course" }
        ]
    },
    {
        "python":
        {
            "occurrences": [
                { "attendees": 24 },
                { "subject": "another crash course" },
                { "notes": "completed with issues" }
            ,
                { "attendees": 30 },
                { "subject": "another crash course" },
                { "notes": "completed with issues" }
            ]
        }
    }
],
"instructors":[
    {
        "John Doe":[
            { "hours": 20 },
            { "experience": 50 },
            { "completed": true }
        ]
    },
    {
        "Anne Baes": [
            { "hours": 45 },
            { "experience": 40 },
            { "completed": false},
            { "prevExperience": true}
        ]
    }
]
}

What I want to do is to print all the data contained in the JSON (I would like something like): 我想要做的是打印JSON中包含的所有数据(我想要类似的东西):

courses
Java
attendees = 43
...
Anne Baes
hours = 45
experience = 40
completed = false
prevExperience = true

I've tried with: 我尝试过:

for(element in data){
    console.log(`element = ${{element}}`);
}

and it only prints: 它只打印:

element = [object Object]
element = [object Object]

(which makes sense because the json is made up of two elements ) (这很有意义,因为json由两个元素组成)

I've tried nesting the line: 我试过嵌套行:

for(element in data){

the problem here is that there is an irregular structure, I mean, "java" and "python" are the same level data but at the same time they have different (array and object) type of value and at the case of 'instructors' they have the same type of value but they're different about them number of values. 这里的问题是有一个不规则的结构,我的意思是,“ java”和“ python”是同一级别的数据,但同时它们具有不同的(数组和对象)值类型,在“讲师”的情况下它们具有相同的值类型,但它们的值数量不同。

Could somebody please help me?:( 有人可以帮我吗?

You can do that using recursion and for..in loop 您可以使用递归for..in循环来实现

 const obj = { "courses": [ { "java": [ { "attendees": 43 }, { "subject": "Crash course" } ] }, { "python": { "occurrences": [ { "attendees": 24 }, { "subject": "another crash course" }, { "notes": "completed with issues" } , { "attendees": 30 }, { "subject": "another crash course" }, { "notes": "completed with issues" } ] } } ], "instructors":[ { "John Doe":[ { "hours": 20 }, { "experience": 50 }, { "completed": true } ] }, { "Anne Baes": [ { "hours": 45 }, { "experience": 40 }, { "completed": false}, { "prevExperience": true} ] } ] }; function print(obj,isArr = false){ for(let key in obj){ if(typeof obj[key] === 'object'){ if(isArr === false) console.log(key) print(obj[key],Array.isArray(obj[key])); } else console.log(`${key} = ${obj[key]}`) } } print(obj) 

You can try iterating over the values using a recursive function like this 您可以尝试使用递归函数迭代这些值

var Obj {

//Contents

}

function printRec(obj){
if(Object.keys(obj).length>0||Array.isArray(obj)){
  for(elem in obj){
  printRec(obj);
  }
}else{
  //Process Value
  console.log(obj)
}
}

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

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