【发布时间】: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”,我不知道下一步该做什么。
【问题讨论】:
-
当您还想要“键”的值时,为什么要
Object.keys()?看看Object.entries()
标签: javascript json object