使用jaxson根值定制来解析json字符串 [英] Parse json string using jaxson root value customization

查看:108
本文介绍了使用jaxson根值定制来解析json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的JSON字符串作为对rest调用的响应. 部分响应具有以下结构. 我正在使用com.fasterxml.jackson

I have a huge JSON string as a response to a rest call. Part of the response has the following structure. I am using com.fasterxml.jackson

"historicalData": {
     "1585573790000": {"score":23.54, "count":3},
     "1585487390000": {"score":12.65, "count":2}
    },
//1585573790000 -> being the epoch time

到目前为止,我想到的模型是

The model I thought of so far is ArrayList of


private class HistoricalData {
        Long epochTime;
        Double score;
        Longcount;

    }

但是我无法映射时间.

推荐答案

您的类HistoricalData与JSON结构不匹配. 您可以使用Map来匹配JSON.

your class HistoricalData does not match the JSON structure. You could use a Map to match the JSON.

Map<Long, HistoryData> historicalData;

然后类HistoryData看起来像这样:

class HistoryData {
    private Double score;
    private Long count;

    // getters & setters
}

然后您可以像这样处理地图:

Then you could process the map like:

historicalData.entrySet().forEach(entry -> {
    // do something with the entry
    // entry.getKey() would return the epochTime
    // entry.getValue() would return the HistoryData object
});

但是,老实说,如果您可以将JSON结构更改为:

But to be honest it would be better if you could change the JSON structure to:

"historicalData": [
  { "epochTime": "1585573790000", "score":23.54, "count":3},
  { "epochTime": "1585487390000", "score":12.65, "count":2}
]

然后您可以使用HistoricalData类的List:

private List<HistoricalData> historicalData;

该类如下:

public class HistoricalData {
    private Long epochTime;
    private Double score;
    private Long count;

    // getters & setters
}

这篇关于使用jaxson根值定制来解析json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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