简体   繁体   中英

Parsing a large JSON data in android and filtering it

Context: I'm requesting large JSON data from my back-end which consists of a constantly updating JSON in a VPS.

I request my first JSON file which contains my movie release dates and the id of the movies; example:

[
  {
   "id": 101,
   "release_date": "2018-03-23",
  }, {...}  
]

The second JSON file I request contains the movie's general info like name and description, example;

[
  {
    "id": 101,
    "name": "PACIFIC RIM UPRISING",
    "description": "Here's a cool description",
    "genres:": [12, 11, 8]
  }, {...}  
]

Both files requested are saved in the user's phone.

Now on to parsing (the subject of this question). I first parse the release JSON in an ArrayList (Release is an object containing the movie ID and the release date), then I parse the movie info JSON in a HashMap (Integer is the id of the movie and Movie is the object containing the name, then genre lists and the description).

Now when I want to filter the releases by genres (user can filter the recyclerview/data by multiple genres at once) I use this code:

    Collections.sort(userGenres); // user chosen genres gotten from a shared pref
    ArrayList<Release> newReleases = new ArrayList<>(); // the list with only the movie the user wants by genre 
    for (Release release : mUpcomingReleases) {
        Movie movie = gameInfoHashMap.get(release.id); // gameInfoHashMap is the hashmap which contains our movie info (it has the genres list)
        ArrayList<Integer> releaseGenres = movie.genres; 
        Collections.sort(releaseGenres);
        if (!releaseGenres.equals(genres)) {
            // if they don't contain the same genres chosen by the user, get out
            break;
        } else {
            // add it to the list
            newReleases.add(release);
        }
    }

And now the problem is it doesn't seem efficient at all. You need to understand that my mUpcomingReleases contains like a thousand releases object, is there a better way to filter by genre(s)? My app feels slow. Or is there a better way to change the parsing of my app to make it more lightweight?

I think what you need to do is, you should not download all the data at once. Only parse like 50-60 objects at a time. Make your backend send you only parts of the complete result. Its the best practice to get only the required data ie only the parts which user can see at a time. As the user scrolls down the recyclerview load data simultaneously. Apps like IMDB and others do the same.

Hope this helps

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