Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
How JavaScript uses variables when reading json

Like the title, I tried to use variables when reading json, but it didn't work (

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);
            }
        }
    })
}
)

I think it read to this:

{
    "lnm": ""
}

But I want read to this(Suppose the lnm variable is BingImage):

{
    "BingImage": "每日Bing图获取"
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Once you make the Ajax call, you parse the JSON and are now able to use it.

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);
            }
        }
    })
}
)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda