A
A
Alexander Vasilenko2016-04-11 06:52:53
Android
Alexander Vasilenko, 2016-04-11 06:52:53

How to parse large JSON?

Hi all. You need to parse a large JSON .
I read this and implemented the following in AsyncTask (so far):

private class JSONLoader extends AsyncTask<String, Void, ArrayList<Artist>> {
        ArrayList<Artist> artistsCollection = new ArrayList<>();
        @Override
        protected ArrayList<Artist> doInBackground(String... params) {
            try{
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(params[0]).build();
                Response response = client.newCall(request).execute();

                if(response.isSuccessful()){
                    InputStream in = response.body().byteStream();
                    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
                    reader.beginArray();
                    while (reader.hasNext()){
                        Artist artist = new Gson().fromJson(reader, Artist.class);
                        artistsCollection.add(artist);
                    }
                    reader.endArray();
                    reader.close();
                }else {
                    throw new IOException("Unexpected code" + response);
                }
                return artistsCollection;

            }catch (IOException e){
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(ArrayList<Artist> artists) {
            setArtists(artists);
        }
    }
}

In any case, the application "crashes" because. very, very, very much memory is required. Please advise how to deal with this.
PS: I can assume that many will advise Jackson. Here, please, if it's not difficult, send more code examples. I looked at a few tutorials, and to be honest, in this curry-flavored mishmash, I did not find any good guides on moving data into POJO objects.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
O
Oleg Gamega, 2016-04-11
@SanchelliosProg

235 KB ..... I would use gson not so big to bother, if you have free time for the sake of interest, you can compare the speed of gson and jacson in this example, I don’t think that the difference will be significant (there is a suspicion that gson will show a better result)
did not work with JsonReader but maybe you are doing something wrong, not so big size, although you can do android:largeHeap="true"

I
IceJOKER, 2016-04-11
@IceJOKER

www.mkyong.com/java/jackson-streaming-api-to-read-...
il2ZVE3oUa3J02J.png

S
Sarrius, 2016-04-11
@sarrius

As data grows, jackson has an edge over gson. choose jackson

M
maratische, 2016-04-21
@maratische

I wrote my own SAX parser analog in a similar situation.
There is no need to load the entire text file of a huge size into memory, you still need some specific data only, so at the output of the stream that receives the data (for example, downloads), we hang up the handler. which processes the incoming data line by line / byte by byte, pulling out only the necessary from them.
And at the output we get only an array with the necessary data, without loading everything into memory.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question