Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!