Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

javascript - Retrieve data from JSON using another JSON

Firstly, cities.json has cities info inside, retrieve the info, add to HTML, easy. Harder task is to use cities.json with list of cities ONLY = ["New York", "London", "..."] etc. and specific city info being stored in New York.json, London.json etc. How to retrieve the data from New York.json using cities.json?

let request = new XMLHttpRequest();
request.open('get', `cities.json`);
request.responseType = "json";
request.onload = () => {
    let show = document.getElementById("box");
    for (let object of request.response) {
        box.innerHTML += `<p>
<a href="https://en.wikipedia.org/wiki/${object.name}">${object.name}</a>
<br>
population: ${object.population}
<br>
area: ${object.area}
</p>`;
    }
}
request.send();

New York.json = 
[
    {
        "name": "New York",
        "population": some integer,
        "area": some integer
    },
]
question from:https://stackoverflow.com/questions/66051055/retrieve-data-from-json-using-another-json

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Suppose you have a site folder with content:
  • index.html
  • cities.json {"cities": ["newYork", "london"]}
  • newYork.json {"name": "New York", "population": 999, "area": 232 }
  • london.json {"name": "London", "population": 999, "area": 232 }
  • ...
  1. You have to find an application server to serve the folder on some port:

    Example: https://www.npmjs.com/package/light-server

    Command: light-server -s . -p 8000

    Browser: http://localhost:8000 will render index.html

  2. You can use also fetch to send HTTP requests and receive responses.


    <html>
    <body>
    <div id="data"></div>
    <script>
        function render(city, data) {
            let name = data.name;
            let population = data.population;
            let element = document.createElement("div");
            element.innerHTML = city + "=> name:" + name + ", population: " + population
            document.getElementById("data").append(element);
        }
        fetch("/cities.json").then(response => {
            response.json().then(json => {
                let cities = json.cities;
                cities.forEach(city => {
                    fetch("/" + city + ".json").then(cityResponse => {
                        cityResponse.json().then(cityJson => {
                            render(city, cityJson)
                        })
                    })
                })
            })
        })
    </script>
    </body>
    </html>
<html>
<body>
  <div id="data"></div>
  <script>
    function render(city, data) {
      let name = data.name;
      let population = data.population;
      let element = document.createElement("div");
      element.innerHTML = city + "=> name:" + name + ", population: " + population
      document.getElementById("data").append(element);
    }
    let citiesRequest = new XMLHttpRequest();
    citiesRequest.onload = function () {
      let json = JSON.parse(this.responseText);
      let cities = json.cities;
      cities.forEach(city => {
        let cityRequest = new XMLHttpRequest();
        cityRequest.onload = function () {
          let cityJson = JSON.parse(this.responseText);
          render(city, cityJson);
        }
        cityRequest.open("GET", "/" + city + ".json", true);
        cityRequest.send();
      })
    }
    citiesRequest.open("GET", "/cities.json", true);
    citiesRequest.send();
</script>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...