首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多层次解析resume.json

多层次解析resume.json
EN

Stack Overflow用户
提问于 2017-02-28 16:48:22
回答 2查看 210关注 0票数 1

我正在尝试解析一个json文件,但我不知道如何循环作为数组的根节点。

resume.json --一个JSON-based standard for resumes --在这里:

代码语言:javascript
复制
{
    "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..."
    }]
}

这就是我到目前为止所知道的:

代码语言:javascript
复制
$.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
    });
});

但是如何检索"work“节点下的公司呢?还是“教育”下的不同机构?我如何处理这些数组呢?

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-28 16:51:55

数据存储在普通的旧式Javascript数组中,因此您可以根据需要遍历它们。例如:

代码语言: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);
    })
});

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-02-28 16:53:40

对于嵌套有限的元素,这里有一个快速的方法(意味着它不是无限的)

代码语言:javascript
复制
$.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);
        })
      }
    });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42504010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档