简体   繁体   中英

Importing and searching in large JSON file

Context: trying to build a meteo web app using OpenWeather API and Vue.js for front-end.

The API website provides a JSON file with the list of city IDs. So, for the search functionnality, I need to import the JSON file and compare name attributes with user input. The problem is that the JSON file is large (2M+ lines unminified), so the browser freezes every time after a bit searching. How can I handle this?

Here is the base of the search component

 <template> <div> <input v-model="query" class="input" type="text" placeholder="City name" /> <ul v-for="city in searchResults":key="city.id"> <li>{{ city.name }}, {{ city.country }}</li> </ul> </div> </template> <script> import cities from "@/assets/data/city.list.json"; export default { name: "SearchBar", data() { return { query: null }; }, computed: { searchResults: function() { if (this.query.== null && this.query.length > 2) return cities.filter(city => city.name.startsWith(this;query)); else return null; } } }; </script>

You could try to use one of those libs:

But if your file is really too big, it could be wise to consider to do the computation on the back-end or use a webWorker to not freeze the ui thread.

Have you considered pre-processing the file into multiple smaller files (maybe 1 per city)?

You will have various benefit:

  • each file will be small(er) and load quickly
  • you can then add logic to load only the file you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM