我正在尝试解析json文件,但我不知道如何循环作为数组的根节点。
resume.json - JSON-based standard for resumes - 就在这里:
{
"basics": {
"name": "John Doe",
"label": "Programmer",
"picture": "",
"email": "john@gmail.com",
"phone": "(912) 555-4321",
"website": "http://johndoe.com",
"summary": "A summary of John Doe...",
"location": {
"address": "2712 Broadway St",
"postalCode": "CA 94115",
"city": "San Francisco",
"countryCode": "US",
"region": "California"
},
"profiles": [{
"network": "Twitter",
"username": "john",
"url": "http://twitter.com/john"
}]
},
"work": [{
"company": "Company",
"position": "President",
"website": "http://company.com",
"startDate": "2013-01-01",
"endDate": "2014-01-01",
"summary": "Description...",
"highlights": [
"Started the company"
]
},
{
"company": "Company",
"position": "President",
"website": "http://company.com",
"startDate": "2013-01-01",
"endDate": "2014-01-01",
"summary": "Description...",
"highlights": []
}
],
"volunteer": [{
"organization": "Organization",
"position": "Volunteer",
"website": "http://organization.com/",
"startDate": "2012-01-01",
"endDate": "2013-01-01",
"summary": "Description...",
"highlights": [
"Awarded 'Volunteer of the Month'"
]
}],
"education": [{
"institution": "University",
"area": "Software Development",
"studyType": "Bachelor",
"startDate": "2011-01-01",
"endDate": "2013-01-01",
"gpa": "4.0",
"courses": [
"DB1101 - Basic SQL"
]
},
{
"institution": "University",
"area": "Software Development 2",
"studyType": "Master",
"startDate": "2014-01-01",
"endDate": "2016-01-01",
"gpa": "6.0",
"courses": []
}
],
"awards": [{
"title": "Award",
"date": "2014-11-01",
"awarder": "Company",
"summary": "There is no spoon."
}],
"publications": [{
"name": "Publication",
"publisher": "Company",
"releaseDate": "2014-10-01",
"website": "http://publication.com",
"summary": "Description..."
}],
"skills": [{
"name": "Web Development",
"level": "Master",
"keywords": [
"HTML",
"CSS",
"Javascript"
]
}],
"languages": [{
"name": "English",
"level": "Native speaker"
}],
"interests": [{
"name": "Wildlife",
"keywords": [
"Ferrets",
"Unicorns"
]
},
{
"name": "Front-end Development",
"level": "",
"keywords": []
}
],
"references": [{
"name": "Jane Doe",
"reference": "Reference..."
}]
}
这是我到目前为止所做的:
$.getJSON('resume.json', function(data) {
console.log(data);
$.each(data, function(i, item) {
var name = this.name;
var label = this.label;
console.log(name); // works
console.log(label); // works
});
});
但是如何在节点“工作”下检索公司?还是“教育”下的不同机构?我该如何处理这些数组?
提前致谢。
答案 0 :(得分:1)
数据存储在普通的旧Javascript数组中,因此您可以根据需要循环遍历它们。例如:
$.getJSON('resume.json', function(data) {
// how can I retrieve the companies under the node "work"?
data.work.forEach(function(w) {
console.log(w.company);
})
// Or the different institutions under "education"?
data.education.forEach(function(e) {
console.log(e.institution);
})
});
var data = {
"basics": {
"name": "John Doe",
"label": "Programmer",
"picture": "",
"email": "john@gmail.com",
"phone": "(912) 555-4321",
"website": "http://johndoe.com",
"summary": "A summary of John Doe...",
"location": {
"address": "2712 Broadway St",
"postalCode": "CA 94115",
"city": "San Francisco",
"countryCode": "US",
"region": "California"
},
"profiles": [{
"network": "Twitter",
"username": "john",
"url": "http://twitter.com/john"
}]
},
"work": [{
"company": "Company 1",
"position": "President",
"website": "http://company.com",
"startDate": "2013-01-01",
"endDate": "2014-01-01",
"summary": "Description...",
"highlights": [
"Started the company"
]
}, {
"company": "Company 2",
"position": "President",
"website": "http://company.com",
"startDate": "2013-01-01",
"endDate": "2014-01-01",
"summary": "Description...",
"highlights": []
}],
"volunteer": [{
"organization": "Organization",
"position": "Volunteer",
"website": "http://organization.com/",
"startDate": "2012-01-01",
"endDate": "2013-01-01",
"summary": "Description...",
"highlights": [
"Awarded 'Volunteer of the Month'"
]
}],
"education": [{
"institution": "University 1",
"area": "Software Development",
"studyType": "Bachelor",
"startDate": "2011-01-01",
"endDate": "2013-01-01",
"gpa": "4.0",
"courses": [
"DB1101 - Basic SQL"
]
}, {
"institution": "University 2",
"area": "Software Development 2",
"studyType": "Master",
"startDate": "2014-01-01",
"endDate": "2016-01-01",
"gpa": "6.0",
"courses": []
}],
"awards": [{
"title": "Award",
"date": "2014-11-01",
"awarder": "Company",
"summary": "There is no spoon."
}],
"publications": [{
"name": "Publication",
"publisher": "Company",
"releaseDate": "2014-10-01",
"website": "http://publication.com",
"summary": "Description..."
}],
"skills": [{
"name": "Web Development",
"level": "Master",
"keywords": [
"HTML",
"CSS",
"Javascript"
]
}],
"languages": [{
"name": "English",
"level": "Native speaker"
}],
"interests": [{
"name": "Wildlife",
"keywords": [
"Ferrets",
"Unicorns"
]
}, {
"name": "Front-end Development",
"level": "",
"keywords": []
}],
"references": [{
"name": "Jane Doe",
"reference": "Reference..."
}]
}
// how can I retrieve the companies under the node "work"?
data.work.forEach(function(w) {
console.log(w.company);
})
// Or the different institutions under "education"?
data.education.forEach(function(e) {
console.log(e.institution);
})
答案 1 :(得分:0)
这是一种快速的方法,用于具有有限嵌套的元素(意味着它不是无限的)
$.getJSON('resume.json', function(data) {
console.log(data);
$.each(data, function(i, item) {
//you already have the key in 'i'
if(i === "work") {
//item is the actual value of "work"
$.each(item, function(j, value) {
console.log(value);
})
}
});
});