gpt4 book ai didi

java - JSON 解析花费太多时间 - Android

转载 作者:太空狗 更新时间:2023-10-29 16:13:37 27 4
gpt4 key购买 nike

我有一个包含 5720 个对象的 JSON 文件 airports.json,我想将该文件用 Java 解析为 Objects Airport。下面的代码是我解析它的方式,问题是,完全解析所有文件需要花费太多时间,大约 1 分 50 秒。

    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()));

有没有办法更快地解析它。顺便说一下,我的 friend 正在使用 Objective C 毫不费力地解析它。

Link to JSON file

最佳答案

使用GSON相反,并用它测量速度。虽然仅读取文件可能会花费很长时间,但整整一分钟似乎很糟糕。

既然你提到你不知道如何使用 GSON,here's a tutorial I wrote for a student on how to use GSON .它假定您是从网络调用中获取文件的,因此您需要应用它来使用本地 JSON 文件。

关于java - JSON 解析花费太多时间 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36313626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com