【问题标题】:How to get to object inside object - json [duplicate]如何获取对象内部的对象 - json [重复]
【发布时间】:2021-04-10 02:38:26
【问题描述】:

我的 json 文件有问题。我的主对象有另一个对象,但我无法访问该对象。

"vendors": {
"1": {
  "id": 1,
  "name": "Exponential Interactive, Inc d/b/a VDX.tv",
  "policyUrl": "https://vdx.tv/privacy/",
},
"2": {
  "id": 2,
  "name": "Captify Technologies Limited",
  "policyUrl": "https://www.captify.co.uk/privacy-policy-opt/",
},
"4": {
  "id": 4,
  "name": "Roq.ad Inc.",
  "policyUrl": "https://www.roq.ad/privacy-policy",
},

我需要获取“名称”属性并将其显示在li 标记中。我写了这样的东西:

fetch("jsonpage")
  .then((res) => res.json())
  .then((data) => {
    for (const number of Object.keys(data.vendors)) {
      let newVendor = document.createElement("li");
      newVendor.textContent = number;
      ulList.appendChild(newVendor);
    }
  })
  .catch(console.error);

但我只能访问数字“1”、“2”、“4”,我不知道下一步该做什么。

【问题讨论】:

标签: javascript json object


【解决方案1】:

您可以按如下方式按键访问该对象。

Objects.keys(data.vendors).forEach((key) => {
  console.log(data.vendors[key]);
  console.log(data.vendors[key].name);
})

【讨论】:

    【解决方案2】:

    我没有完整的代码,但我相信它可能会起作用。使用数字作为数组供应商的索引:

    fetch("jsonpage")
      .then((res) => res.json())
      .then((data) => {
        for (const number of Object.keys(data.vendors)) {
          let newVendor = document.createElement("li");
          newVendor.textContent = data.vendors[number].name;
          ulList.appendChild(newVendor);
        }
      })
      .catch(console.error);
    

    【讨论】:

      【解决方案3】:

      您可以简单地使用for in 循环遍历data.vendors

      const data = {
        vendors: {
          '1': {
            id: 1,
            name: 'Exponential Interactive, Inc d/b/a VDX.tv',
            policyUrl: 'https://vdx.tv/privacy/',
          },
          '2': {
            id: 2,
            name: 'Captify Technologies Limited',
            policyUrl: 'https://www.captify.co.uk/privacy-policy-opt/',
          },
          '4': {
            id: 4,
            name: 'Roq.ad Inc.',
            policyUrl: 'https://www.roq.ad/privacy-policy',
          },
        },
      };
      
      for (const key in data.vendors) {
        console.log("id", data.vendors[key].id)
        console.log("name", data.vendors[key].name)
        console.log("url", data.vendors[key].policyUrl)
      }

      【讨论】:

        【解决方案4】:

        你可以试试这个:

        fetch("jsonpage")
          .then((res) => res.json())
          .then((data) => {
            for (const number of Object.keys(data.vendors)) {
              let newVendor = document.createElement("li");
              newVendor.textContent = number;
              ulList.appendChild(newVendor);
              //then let's get another things;
              let name = data.vendors[number]['name']
              let policyurl = data.vendors[number]['policyUrl']
            }
          })
          .catch(console.error);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-11
          • 1970-01-01
          • 2015-02-22
          • 1970-01-01
          • 2021-12-08
          • 1970-01-01
          • 1970-01-01
          • 2017-03-27
          相关资源
          最近更新 更多