Al igual que el título, traté de usar variables al leer json, pero no funcionó (
fetch('https://lolimstatic.ml/counts/apis.json').then( (response) => { response.json().then((data) => { const apiListFrame = document.querySelector('#als'); const api = document.createElement('a'); api.classList.add('btns'); api.classList.add('button'); api.classList.add('apilist'); var rns = Math.round(Math.random()*10); colors = ["blue","red","white","orange","pink","green","yellow","skyblue","purple","gray"]; api.style.background = colors[rns]; api.style.width = '200px'; var lstd = Math.round(Math.random()*data[0].counts); api.href = 'https://docs.lolimapis.ml/API/'+ data[0].list[lstd] +'/'; lnm = data[0].list[lstd]; var xhr = new XMLHttpRequest; xhr.open("GET", 'https://lolimstatic.ml/counts/apis/zh.json'); xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState = 4) { const res = xhr.responseText; // Here is variables Json api.innerText = res.lnm; apiListFrame.appendChild(api); } } }) } )Creo que decía esto:
{ "lnm": "" }Pero quiero leer esto (supongamos que la variable lnm es BingImage):
{ "BingImage": "每日Bing图获取" }Una vez que realiza la llamada Ajax, analiza el JSON y ahora puede usarlo.
fetch('https://lolimstatic.ml/counts/apis.json').then( (response) => { response.json().then((data) => { const apiListFrame = document.querySelector('#als'); const api = document.createElement('a'); api.classList.add('btns'); api.classList.add('button'); api.classList.add('apilist'); var rns = Math.round(Math.random()*10); colors = ["blue","red","white","orange","pink","green","yellow","skyblue","purple","gray"]; api.style.background = colors[rns]; api.style.width = '200px'; var lstd = Math.round(Math.random()*data[0].counts); api.href = 'https://docs.lolimapis.ml/API/'+ data[0].list[lstd] +'/'; lnm = data[0].list[lstd]; var xhr = new XMLHttpRequest; xhr.open("GET", 'https://lolimstatic.ml/counts/apis/zh.json'); xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { // Here is us parsing the JSON const res = JSON.parse(xhr.responseText);; // Here is variables Json api.innerText = res.GitHubUserInfo; apiListFrame.appendChild(api); } } }) } )