简体   繁体   中英

How to access complicated JSON array & object data?

I am stuck in accessing complicated JSON data for the application in angular, and this is how it look.

{
"members": [
{
"member_id": "1",
"first_name": "John",
"last_name": "Doe",
"education_and_career": "[{\"highest_education\":\"MSc\",\"occupation\":\"Lead Developer\",\"annual_income\":\"100,000 USD\"}]",
}]};

I can access member_id, first name using data.member_id but I am not able to access data.education_and_career.highest_education value. I know it may look very simple but I am stuck so I really appreciate your time.

Thanks in advnace.

"education_and_career" is a field that contains a JSON string therefore you can't use it like a normal array. you can however use the JSON.parse method in order to parse it. so in order to access the hieghest_education field you would do it like so:

JSON.parse(data.education_and_career)[0].highest_education

You need to parse education_and_career property to JSON. Below is sample with map function or for loop

var data = {
  "members": [{
    "member_id": "1",
    "first_name": "John",
    "last_name": "Doe",
    "education_and_career": "[{\"highest_education\":\"MSc\",\"occupation\":\"Lead Developer\",\"annual_income\":\"100,000 USD\"}]",
  }]
};
var parsed=data.members.map(i=>JSON.parse(i.education_and_career));
console.log("parsed",parsed)

for (i = 0; i < data.members.length; i++) {
  console.log(JSON.parse(data.members[i].education_and_career))
}

It looks like education_and_career is not an object array. You need to use the String.replace() method to remove \\ and convert it to JSON data and you would be able to access the highest_education value like education_and_career[0].highest_enducation_value or so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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