简体   繁体   中英

JSON Parsing taking too much time - Android

I have a JSON file airports.json which contains 5720 objects, I want to parse the file in Java into Objects Airport. The code below is how I parse it, the problem is, it is taking too much time to completely parse all the file, about 1 min and 50 sec.

    ArrayList<Airport> arrayAirports = new ArrayList<>();
    String json_response = loadJSONFromAsset();
    try {
        JSONObject airports = new JSONObject(json_response.trim());

        Log.d("Length Array 0", String.valueOf(airports.names().length()));
        Log.d("Length Array 1", String.valueOf(arrayAirports.size()));
        for(int i = 0; i<airports.names().length(); i++){
                JSONObject jsonAirport = airports.getJSONObject(airports.names().getString(i));
                Airport newAirport = new Airport();
                newAirport.name = jsonAirport.getString("name");
                newAirport.city = jsonAirport.getString("city");
                newAirport.country = jsonAirport.getString("country");
                newAirport.latitude = jsonAirport.getString("latitude");
                newAirport.longitude = jsonAirport.getString("longitude");
                newAirport.altitude = jsonAirport.getString("altitude");
                newAirport.iata = jsonAirport.getString("iata");
                newAirport.icao = jsonAirport.getString("icao");
                newAirport.timeZone = jsonAirport.getString("timezone");
                newAirport.dst = jsonAirport.getString("dst");
                arrayAirports.add(newAirport);
        }



    } catch (JSONException e1) {
        e1.printStackTrace();
    }

    Log.d("Length Array 2", String.valueOf(arrayAirports.size()));

Is there a way to parse it quicklier. By the way my friend is parsing it with no time at all using Objective C.

Link to JSON file

Use GSON instead and measure the speed with it. While it is possible possible that just reading the file could take a long time, a whole minute seems pretty bad.

Since you mentioned you don't know how to use GSON, here's a tutorial I wrote for a student on how to use GSON . It assume you're getting the file from a network call though, so you need to apply it to use your local JSON file.

Don't know if it matters for performance, but you shouldn't call names() repeatedly. Assign to variable before loop, then use it.

JSONArray names = airports.names();
Log.d("Length Array 0", String.valueOf(names.length()));
Log.d("Length Array 1", String.valueOf(arrayAirports.size()));
for(int i = 0; i<names.length(); i++){
    JSONObject jsonAirport = airports.getJSONObject(names.getString(i));
    // code
}

Better yet, use length() and keys() :

Log.d("Length Array 0", String.valueOf(airports.length()));
Log.d("Length Array 1", String.valueOf(arrayAirports.size()));
for (Iterator<String> nameIter = airports.keys(); nameIter.hasNext(); ){
    String name = nameIter.next();
    JSONObject jsonAirport = airports.getJSONObject(name);
    // code
}

You are putting a (I guess?) large JSON data file into a JSONObject directly.

In this case it would be recommended to use a token based reader, such as JsonReader .

Pasted directly from the docs:

public List readJsonStream(InputStream in ) throws IOException {
    JsonReader reader = new JsonReader(new InputStreamReader( in , "UTF-8"));
    try {
        return readMessagesArray(reader);
        finally {
            reader.close();
        }
    }

    public List readMessagesArray(JsonReader reader) throws IOException {
        List messages = new ArrayList();

        reader.beginArray();
        while (reader.hasNext()) {
            messages.add(readMessage(reader));
        }
        reader.endArray();
        return messages;
    }

    public Message readMessage(JsonReader reader) throws IOException {
        long id = -1;
        String text = null;
        User user = null;
        List geo = null;

        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equals("id")) {
                id = reader.nextLong();
            } else if (name.equals("text")) {
                text = reader.nextString();
            } else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
                geo = readDoublesArray(reader);
            } else if (name.equals("user")) {
                user = readUser(reader);
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
        return new Message(id, text, user, geo);
    }

    public List readDoublesArray(JsonReader reader) throws IOException {
        List doubles = new ArrayList();

        reader.beginArray();
        while (reader.hasNext()) {
            doubles.add(reader.nextDouble());
        }
        reader.endArray();
        return doubles;
    }

    public User readUser(JsonReader reader) throws IOException {
        String username = null;
        int followersCount = -1;

        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equals("name")) {
                username = reader.nextString();
            } else if (name.equals("followers_count")) {
                followersCount = reader.nextInt();
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
        return new User(username, followersCount);
    }
}

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