【发布时间】:2021-11-02 12:59:29
【问题描述】:
我正在使用 JSON 从 json 文件中存储和检索一些值。我正在尝试使用 javascript 获取 JSON 数据。 JSON 数据在数组列表中有数组。我能够获取对象列表但无法获取内部数组。下面是我的 JSON 数据及其格式。 我错过了什么?
artifacts.json
{
"artifacts": [
{
"technology": "Agile Software Development",
"techBasedArtifacts": [
{
"title": "Agile 1",
"artifactLink": "https://www.google.com/"
},
{
"title": "Agile 2",
"artifactLink": "https://www.google.com/"
}
]
},
{
"technology": "UI Development",
"techBasedArtifacts": [
{
"title": "UI 1",
"artifactLink": "https://www.google.com/"
},
{
"title": "UI 2",
"artifactLink": "https://www.google.com/"
}
]
}
]
}
app.js
"use-strict";
let requestURL = "./artifacts.json";
let request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'text';
request.send();
request.onload = () => {
const a = request.response;
const b = JSON.parse(a);
console.log(b.artifacts) //Shows object array which is good
console.log(b.artifacts.techBasedArtifacts) // Shows undefined
}
【问题讨论】:
-
您需要 iterate over the
artifactsarray 并为每个对象获取techBasedArtifacts数组。然后,您可能还需要根据您需要的信息迭代该数组。
标签: javascript arrays json object arraylist