gpt4 book ai didi

javascript - 尝试访问 json 内容并以网格方式显示

转载 作者:行者123 更新时间:2023-11-30 08:20:43 24 4
gpt4 key购买 nike

我有一个包含以下内容的 JSON 文件。

{
"rooms":[
{
"id": "1",
"name": "living",
"Description": "The living room",
"backgroundpath":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrsU8tuZWySrSuRYdz72WWFiYaW5PCMIwPAPr_xAIqL-FgmQ4qRw",
"Settings": {
"Sources": [
{
"srcname":["DirectTV","AppleTV","Sonos"],
"iconpath":["src/assets/images/ThemeEditorImgLib_iTVLight5d3a46c1ad5d7796.png","path2"]
}
],
"hex": "#000"
}
},
{
"id": "2",
"name": "dining",
"Description": "The Dining room",
"backgroundpath":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTjjspDkQEzbJJ1sSLS2ReWEx5P2ODNQmOtT572OIF9k3HbTGxV",
"Settings": {
"Sources": [
{
"srcname":["Climate","Shades"],
"iconpath":["path1","path2"]
}
],
"hex": "#000"
}
}
]
}

我的 HTML 文件有:

<div class="roww">
<div class="col-md-4 col-sm-6 col-12">

</div>
</div>

我想在 HTML 中显示我的 JSON 内容。到目前为止,我已经尝试了以下。

private initTileGrid():void
{

var col = document.getElementsByClassName("roww")[0];

var outeritems=this.room_list.rooms;
// console.log(outeritems);
for(var i=0;i<outeritems.length;i++){
var tile = document.getElementsByClassName("col-md-4 col-sm-6 col-12 tile no-padding")[0];
var outerdiv = document.createElement("div");
var h5 = document.createElement("h5");
h5.innerHTML = outeritems[i].Description;
outerdiv.appendChild(h5);
var p = document.createElement("p");
p.innerHTML = outeritems[i].name;
outerdiv.appendChild(p);
// col.appendChild(outerdiv);
tile.appendChild(outerdiv);
var inneritem=outeritems[i].Settings.Sources.srcname;
console.log(inneritem);
var innerdiv = document.createElement("div");
for(var j=0;j<inneritem.length;j++)
{
console.log("hi")
var h5inner = document.createElement("h5");
h5inner.innerHTML = inneritem.srcname;
console.log(h5inner);
innerdiv.appendChild(h5inner);
}
tile.appendChild(innerdiv);
}

我能够显示来自 json 的描述和名称,但无法获取来源列表。

我的最终解决方案应该是一个网格,其中的图 block 数量等于 JSON 中的对象数量,每个图 block 都有一个来自 json 及其内容的背景图像。

谁能告诉我哪里出错了?任何帮助表示赞赏!

最佳答案

我建议并推荐使用Template Literals 以下列方式生成您的动态HTML .

使用 Array#map , Array#joinDestructuring assignment

const data = {"rooms":[{"id":"1","name":"living","Description":"The living room","backgroundpath":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrsU8tuZWySrSuRYdz72WWFiYaW5PCMIwPAPr_xAIqL-FgmQ4qRw","Settings":{"Sources":[{"srcname":["DirectTV","AppleTV","Sonos"],"iconpath":["src/assets/images/ThemeEditorImgLib_iTVLight5d3a46c1ad5d7796.png","path2"]}],"hex":"#000"}},{"id":"2","name":"dining","Description":"The Dining room","backgroundpath":"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTjjspDkQEzbJJ1sSLS2ReWEx5P2ODNQmOtT572OIF9k3HbTGxV","Settings":{"Sources":[{"srcname":["Climate","Shades"],"iconpath":["path1","path2"]}],"hex":"#000"}}]}

const res = data.rooms.map(({name, Description, Settings:{Sources}})=>{

const inner = Sources.map(({srcname})=>{
return srcname.map(src=>`<h5>${src}</h5>`).join("");
}).join("");

return `
<div>
<h5>${Description}</h5>
<p>${name}</p>
<div class="inner">
${inner}
</div>
</div>
`
}).join("");

document.body.innerHTML = res;
body>div{background-color:lightgrey;padding:5px;margin-top:5px}body>div::before{content:"row"}body>div>*{margin-left:10px}h5{background-color:blue;color:white;padding:5px}h5::before{content:"outer h5:: "}p{background-color:purple;color:white;padding:5px}p::before{content:"p:: "}div.inner{padding:5px;background-color:grey}div.inner::before{content:"inner div"}div.inner>h5{background-color:green;color:white}div.inner>h5::before{content:"inner h5:: "}

未压缩的 CSS:

body > div {
background-color: lightgrey;
padding: 5px;
margin-top: 5px;
}

body > div::before {
content: "row";
}

body > div > * {
margin-left: 10px;
}

h5 {
background-color: blue;
color: white;
padding: 5px;
}

h5::before {
content: "outer h5:: "
}

p {
background-color: purple;
color: white;
padding: 5px;
}

p::before {
content: "p:: ";
}

div.inner {
padding: 5px;
background-color: grey;
}

div.inner::before {
content: "inner div";
}

div.inner > h5 {
background-color: green;
color: white;
}

div.inner > h5::before {
content: "inner h5:: "
}

关于javascript - 尝试访问 json 内容并以网格方式显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54116952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com