【问题标题】:Fetching JSON data from array's array list in javascript在javascript中从数组的数组列表中获取JSON数据
【发布时间】: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 artifacts array 并为每个对象获取 techBasedArtifacts 数组。然后,您可能还需要根据您需要的信息迭代该数组。

标签: javascript arrays json object arraylist


【解决方案1】:

IMO 工件是一个数组,但不是一个对象。您正在从对象访问“techBasedArtifacts”字段:

console.log(b.artifacts[0].techBasedArtifacts);

【讨论】:

  • 我错过了为工件提供索引,而是一直尝试使用技术和 techBasedArtifacts 进行索引。谢谢您的回答。成功了!
  • 是的,您是正确的,artifacts 是一个数组,而 techBasedArtifacts 字段来自对象。如何获取与 UI Development-> UI 1、UI 2 等技术相关的标题值。我尝试使用 b.artifacts[i].techBasedArtifacts[i].title,它显示第一个数组的第一个标题和第二个数组的第二个标题大批。我的 json 格式有什么改变吗?感谢您的宝贵时间!
【解决方案2】:

如果你想获取特定索引的 techBasedArtifacts,那么你可以使用 like

console.log(b.artifacts[0].techBasedArtifacts)

您可以访问第 0 个索引的子数组。

如果要获取所有索引的所有 techBasedArtifacts,则必须迭代数组并将所有 techBasedArtifacts 合并到单个数组中。

var result = b.artifacts.map(x => x.techBasedArtifacts).reduce((x, y) => { return x = [...x, ...y];})
console.log(result); // will give you merged result set.

【讨论】:

  • 感谢您的回答! :) 考虑到您的第一个答案,如果我想单独获得 techBasedArtifacts 标题,我尝试使用 artifacts[0].techBasedArtifacts.title 并且它给出了未定义的。所以我尝试使用 JSON.stringify 但它显示了 techBasedArtifacts 下的所有内容。如何带上 techBasedArtifacts 标题?感谢您的宝贵时间!
  • 由于 techBasedArtifacts 也是一个数组,你只能通过索引来访问它,所以你必须像 artifacts[0].techBasedArtifacts[0].title 一样调用它。
  • 我玩弄了你的建议,并通过像 artifact[i].techBasedArtifacts[i].title 显示第一个数组的第一个标题和第二个数组的第二个标题来获取 techBasedArtifacts 标题。但我试图获得与技术相关的 techBasedArtifacts 标题,我的 json 格式有什么问题吗?感谢您的宝贵时间!
  • 如果您希望所有 techBasedArtifacts 采用逗号分隔格式,如 UI 1、UI 2,那么您可以简单地映射元素以获取标题,这将为您提供一系列标题,然后您可以简单地使用连接使用逗号作为分隔符的方法将其转换为单个字符串。例如:b.artifacts[0].techBasedArtifacts.map(x => x.title).join(', '),希望对您的需求有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多