如何使用节点js迭代不规则的嵌套json
我正在Nodejs做一个小应用程序,我正在努力尝试循环一个不规则的JSON来打印它的数据。
我的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}
]
}
]
}
我想要做的是打印JSON中包含的所有数据(我想要的东西):
courses
Java
attendees = 43
...
Anne Baes
hours = 45
experience = 40
completed = false
prevExperience = true
我尝试过:
for(element in data){
console.log(`element = ${{element}}`);
}
它只打印:
element = [object Object]
element = [object Object]
(这是有道理的,因为json由两个元素组成)
我试过嵌套线:
for(element in data){
这里的问题是有一个不规则的结构,我的意思是,“java”和“python”是相同级别的数据,但同时它们具有不同的(数组和对象)类型的值,并且在“教师”的情况下它们具有相同类型的价值,但它们的价值数量不同。
有人可以帮帮我吗?:(
回答如下:你可以使用递归和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)
如何使用节点js迭代不规则的嵌套json
我正在Nodejs做一个小应用程序,我正在努力尝试循环一个不规则的JSON来打印它的数据。
我的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}
]
}
]
}
我想要做的是打印JSON中包含的所有数据(我想要的东西):
courses
Java
attendees = 43
...
Anne Baes
hours = 45
experience = 40
completed = false
prevExperience = true
我尝试过:
for(element in data){
console.log(`element = ${{element}}`);
}
它只打印:
element = [object Object]
element = [object Object]
(这是有道理的,因为json由两个元素组成)
我试过嵌套线:
for(element in data){
这里的问题是有一个不规则的结构,我的意思是,“java”和“python”是相同级别的数据,但同时它们具有不同的(数组和对象)类型的值,并且在“教师”的情况下它们具有相同类型的价值,但它们的价值数量不同。
有人可以帮帮我吗?:(
回答如下:你可以使用递归和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)