提高json解析性能 [英] Improve json parse performance

查看:165
本文介绍了提高json解析性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析来自此api的json数据(摘自其示例页面):

I'd like to parse the json data coming from this api (taken from their example page):

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

我确实建立了一种解析方法,该解析方法需要花费一定的时间(在我的Galaxy A5上为几秒钟)来解析数据(我知道要解析的数据很多):

I did build a parse method which takes ages (several seconds on my Galaxy A5) to parse the data (i know it is a lot of data to parse):

这就是我所做的:

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库?

What are the best improvements? Switching to another JSON library?

推荐答案

使用 Gson 进行解析和 JsonSchemaToPojo 来创建POJO类.

Use Gson for parsing and JsonSchemaToPojo for creating POJO classes .

这篇关于提高json解析性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆