Javascript 如何将JSON响应附加到HTML网格

Javascript 如何将JSON响应附加到HTML网格,javascript,json,pagespeed,Javascript,Json,Pagespeed,如何将此JSON数据附加到例如Grid布局中 我正在通过以下脚本提取Google PageSpeed JSON数据: 函数已单击(){ 常量url=document.getElementById('url')。值; document.getElementById('urlerror')。style.display='none'; if(url.indexOf('https://')=-1){ document.getElementById('urlerror').style.display='b

如何将此
JSON
数据附加到例如
Grid
布局中

我正在通过以下脚本提取
Google PageSpeed JSON
数据:

函数已单击(){
常量url=document.getElementById('url')。值;
document.getElementById('urlerror')。style.display='none';
if(url.indexOf('https://')=-1){
document.getElementById('urlerror').style.display='block';
返回;
}
const xhr=new XMLHttpRequest();
xhr.open('GET'`https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=${encodeURIComponent(url)}&fields=lighthouse结果%2fcombicates%2F*%2Fscore&prettyPrint=false&strategy=desktop&category=performance&category=pwa&category=best practices&category=accessibility&category=seo&key={YOUR_API_key}`);
xhr.onload=函数(){
document.getElementById('data').innerHTML=xhr.responseText;
}
xhr.send();
}
JSON数据被发送到以下
div


请输入有效的URL,例如。http://www.example.com
这是JSON数据。类别保持不变,但分数是动态的:

{
  "lighthouseResult": {
    "categories": {
      "performance": {
        "score": 0.99
      },
      "accessibility": {
        "score": 0.7
      },
      "best-practices": {
        "score": 0.77
      },
      "seo": {
        "score": 0.9
      },
      "pwa": {
        "score": 0.56
      }
    }
  }
}
我想看两列,左边是分类,右边是分数。每个类别和分数在HTML中都有自己的id或类,可能如下所示:

函数可渲染(json){
console.log(json)
//TODO:删除伪数据!并从json中设置数据!
常数数据={
“灯塔结果”:{
“类别”:{
“业绩”:{
“分数”:0.99
},
“无障碍”:{
“分数”:0.7
},
“最佳做法”:{
“分数”:0.77
},
“搜索引擎优化”:{
“分数”:0.9
},
“pwa”:{
“分数”:0.56
}
}
}
}
const{categories}=data.lighthouse结果
const table=document.createElement('表')
const trHeading=document.createElement('tr')
const thCategory=document.createElement('th')
trHeading.classList='category heading'
thCategory.innerText='Category'
thCategory.classList='category key heading'
trHeading.appendChild(thCategory)
const thScore=document.createElement('th')
thScore.innerText='Score'
thScore.classList='类别值标题'
trHeading.appendChild(thScore)
表2.appendChild(trHeading)
Object.key(categories).forEach(categority=>{
const tr=document.createElement('tr')
tr.classList='类别-'+类别
const tdKey=document.createElement('td')
tdKey.innerText=类别
tdKey.classList='category key category key-'+category
tr.appendChild(tdKey)
const tdValue=document.createElement('td')
tdValue.innerText=类别[category]。分数
tdValue.classList='类别值类别值-'+类别
tr.appendChild(tdValue)
表2.追加子项(tr)
})
const out=document.getElementById(“数据”)
out.innerHTML=“”
out.appendChild(表)
}
函数单击(){
document.getElementById(“urlerror”).style.display='none';
var url=document.getElementById(“url”).value;
if(url.indexOf('https://')=-1{document.getElementById(“urlerror”).style.display='block';return;}
var xhr=newXMLHttpRequest()
xhr.open(“GET”https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=“+encodeURIComponent(url)+”&fields=Lighthouse结果%2Fcategories%2F*%2Fscore&prettyPrint=false&strategy=desktop&categority=performance&categority=pwa&categories=best practices&categories=accessibility&categority=seo&key={YOUR_API_key}”)
xhr.onload=函数(){
//document.getElementById(“数据”).innerHTML=xhr.responseText
可渲染(xhr.response)
}
xhr.send()
}

请输入有效的URL,例如。http://www.example.com
可能是这样的:

函数可渲染(json){
console.log(json)
//TODO:删除伪数据!并从json中设置数据!
常数数据={
“灯塔结果”:{
“类别”:{
“业绩”:{
“分数”:0.99
},
“无障碍”:{
“分数”:0.7
},
“最佳做法”:{
“分数”:0.77
},
“搜索引擎优化”:{
“分数”:0.9
},
“pwa”:{
“分数”:0.56
}
}
}
}
const{categories}=data.lighthouse结果
const table=document.createElement('表')
const trHeading=document.createElement('tr')
const thCategory=document.createElement('th')
trHeading.classList='category heading'
thCategory.innerText='Category'
thCategory.classList='category key heading'
trHeading.appendChild(thCategory)
const thScore=document.createElement('th')
thScore.innerText='Score'
thScore.classList='类别值标题'
trHeading.appendChild(thScore)
表2.appendChild(trHeading)
Object.key(categories).forEach(categority=>{
const tr=document.createElement('tr')
tr.classList='类别-'+类别
const tdKey=document.createElement('td')
tdKey.innerText=类别
tdKey.classList='category key category key-'+category
tr.appendChild(tdKey)
const tdValue=document.createElement('td')
tdValue.innerText=类别[category]。分数
tdValue.classList='类别值类别值-'+类别
tr.appendChild(tdValue)
表2.追加子项(tr)
})
const out=document.getElementById(“数据”)
out.innerHTML=“”
out.appendChild(表)
}
函数单击(){
document.getElementById(“urlerror”).style.display='none';
var url=document.getElementById(“url”).value;
if(url.indexOf('https://')=-1{document.getElementById(“urlerror”).style.display='block';return;}
var xhr=newXMLHttpRequest()
xhr.open(“GET”https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=“+编码元件