Answer the question
In order to leave comments, you need to log in
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);
}
}
}
Answer the question
In order to leave comments, you need to log in
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 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 questionAsk a Question
731 491 924 answers to any question