gpt4 book ai didi

android - 提高json解析性能

转载 作者:行者123 更新时间:2023-11-29 01:01:27 24 4
gpt4 key购买 nike

我想解析来自这个 api 的 json 数据(取自他们的示例页面):

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=full&apikey=demo

我确实构建了一个解析方法,它需要很长时间(在我的 Galaxy A5 上需要几秒钟)来解析数据(我知道要解析的数据很多):

这是我做的:

private static List<PriceInfo> parsePriceDaily(JSONObject response, boolean onlyCurrentPrice) throws JSONException {
long millis = System.currentTimeMillis();
/* Check if this message is an Error Message = No data for symbol available.
* If not fetch data
* example structure
* https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=full&apikey=demo
*/

/* Get the First Object - Either Meta Data or Error Message */
Iterator<String> it = response.keys();
String key = it.next();
/* Is Error? */
if (key.contains("Error Message")) {
throw new JSONException(response.getString(key));
}
/* Valid Object - process metadata */
String timeZone = response.getJSONObject(key).getString("5. Time Zone");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
format.setTimeZone(TimeZone.getTimeZone(timeZone));

/* Process datasets */
List<PriceInfo> result = new ArrayList<>();
key = it.next();
JSONObject datasets = response.getJSONObject(key); /* Time Series (Daily) */
JSONObject dataset;
String dateString;
for (it = datasets.keys(); it.hasNext(); ) {
dateString = it.next(); /* dataset */
dataset = datasets.getJSONObject(dateString);
Date date = new Date(0); /* standard value */
try {
date = format.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
result.add(
new PriceInfo(
date,
dataset.getString("1. open"),
dataset.getString("2. high"),
dataset.getString("3. low"),
dataset.getString("4. close"),
dataset.getString("5. adjusted close"),
dataset.getString("6. volume"),
dataset.getString("7. dividend amount"),
dataset.getString("8. split coefficient")
)
);
if (onlyCurrentPrice) {
break;
}
}
Log.d(TAG, "Passed time: " + (System.currentTimeMillis() - millis));
return result;
}

最好的改进是什么?切换到另一个 JSON 库?

最佳答案

使用Gson用于解析和 JsonSchemaToPojo用于创建 POJO 类。

关于android - 提高json解析性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50879609/

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