[英]How to access complicated JSON array & object data?
我一直在以 angular 訪問應用程序的復雜 JSON 數據,這就是它的外觀。
{
"members": [
{
"member_id": "1",
"first_name": "John",
"last_name": "Doe",
"education_and_career": "[{\"highest_education\":\"MSc\",\"occupation\":\"Lead Developer\",\"annual_income\":\"100,000 USD\"}]",
}]};
我可以使用 data.member_id 訪問 member_id、名字,但我無法訪問 data.education_and_career.highest_education 值。 我知道它可能看起來很簡單,但我被卡住了,所以我非常感謝你的時間。
預先感謝。
"education_and_career"
是一個包含 JSON 字符串的字段,因此您不能像普通數組一樣使用它。 但是,您可以使用JSON.parse
方法來解析它。 因此,為了訪問 hieghest_education 字段,您可以這樣做:
JSON.parse(data.education_and_career)[0].highest_education
您需要將education_and_career 屬性解析為JSON。 下面是帶有 map 函數或 for 循環的示例
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))
}
看起來education_and_career
不是對象數組。 您需要使用 String.replace() 方法來刪除 \\ 並將其轉換為 JSON 數據,您將能夠訪問諸如 education_and_career[0].highest_enducation_value 之類的最高教育值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.