rdavison
rdavison

Reputation: 71

Parse ~1 MB JSON on Android very slow

I have an approximately 1MB JSON file stored in my assets folder that I need to load in my app every time it runs. I find that the built-in JSON parser (org.json) parses the file very slowly, however once it's parsed, I can access and manipulate the data very quickly. I've counted out as many as 7 or 8 seconds from the moment I click on the app to the moment the Activity1 is brought up, but just a few milliseconds to go from Activity1 to Activity2 which depends on data processed from the data loaded in Activity1.

I'm reading the file into memory and parsing it using:

String jsonString = readFileToMemory(myFilename)
JSONArray array = new JSONArray(jsonString);

where readFileToMemory(String) looks like this:

private String readFileToMemory(String filename) {
    StringBuilder data = new StringBuilder();       
    BufferedReader reader = null;
    try {
        InputStream stream = myContext.getAssets().open(filename);
        reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        int result = 0;
        do
        {
            char[] chunk = new char[512];
            result = reader.read(chunk);
            data.append(chunk);
        }
        while(result != -1);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return data.toString();
}

Does anyone have any suggestions to how I can speed up the initial loading and parsing of the data? Should I perhaps mask the whole process behind a loading screen?

Upvotes: 6

Views: 3920

Answers (2)

Nishant
Nishant

Reputation: 55866

JSONObject -- the one from json.org, is the simplest API to use to parse JSON. However it comes with a cost -- performace. I have done extensive experiments with JSONObject, Gson and Jackson. It seems no matter what you do, JSONObject (and hence JSONArray) will be the slowest. Please switch to Jackson or Gson.

Here is the relative performance of the two

(fastest) Jackson > Gson >> JSONObject (slowest)

Refer:
- Jackson
- Gson

Upvotes: 9

mimicocotopus
mimicocotopus

Reputation: 5610

You should make an SQLite table to store the data and move it from JSON to SQL the first time the app runs. As an added benefit, this makes the data easier to search through and makes it possible for you to modify the data from within the app.

Upvotes: 2

Related Questions