【问题标题】:How To Parse JSON Object Array with Jackson Into DTO In SpringBoot如何在 Spring Boot 中将带有 Jackson 的 JSON 对象数组解析为 DTO
【发布时间】:2018-04-26 01:41:51
【问题描述】:

我目前正在构建一个应用程序,该应用程序使用 RestTemplate 来构造对 Alpha Vantage API 的 API 请求。以下是排除不必要的元数据的示例响应:

{
  "Time Series (Daily)": {
    "2017-11-10": {
      "1. open": "83.7900",
      "2. high": "84.1000",
      "3. low": "83.2300",
      "4. close": "83.8700",
      "5. volume": "19340435"
    },
    "2017-11-09": {
      "1. open": "84.1100",
      "2. high": "84.2700",
      "3. low": "82.9000",
      "4. close": "84.0900",
      "5. volume": "20924172"
    },
    "2017-11-08": {
      "1. open": "84.1400",
      "2. high": "84.6100",
      "3. low": "83.8300",
      "4. close": "84.5600",
      "5. volume": "18034170"
    },
    "2017-11-07": {
      "1. open": "84.7700",
      "2. high": "84.9000",
      "3. low": "83.9300",
      "4. close": "84.2700",
      "5. volume": "17152583"
    },
    "2017-11-06": {
      "1. open": "84.2000",
      "2. high": "84.7000",
      "3. low": "84.0800",
      "4. close": "84.4700",
      "5. volume": "19039847"
    },
    "2017-11-03": {
      "1. open": "84.0800",
      "2. high": "84.5400",
      "3. low": "83.4000",
      "4. close": "84.1400",
      "5. volume": "17569120"
    }
  }
}

我正在构建我的数据传输对象层,它的组成如下:

顶层:

public class Stock extends Dto{

    @JsonProperty("Time Series (Daily)")
    private TimeSeries timeSeries;

    public TimeSeries getTimeSeries() {
        return timeSeries;
    }

    public void setTimeSeries(TimeSeries timeSeries) {
        this.timeSeries = timeSeries;
    }
}

中间层:

public class TimeSeries extends Dto{

    private List<Day> days;

    public List<Day> getDays() {
        return days;
    }

    public void setDays(List<Day> days) {
        this.days = days;
    }
}

最后一层(数据输出的地方):

@JsonIgnoreProperties(ignoreUnknown = true)
public class Day extends Dto{

    @JsonProperty("1. open")
    private double open;

    @JsonProperty("2. high")
    private double high;

    @JsonProperty("3. low")
    private double low;

    @JsonProperty("4. close")
    private double close;

    @JsonProperty("5. volume")
    private double volume;

    public double getOpen() {
        return open;
    }

    public void setOpen(double open) {
        this.open = open;
    }

    public double getHigh() {
        return high;
    }

    public void setHigh(double high) {
        this.high = high;
    }

    public double getLow() {
        return low;
    }

    public void setLow(double low) {
        this.low = low;
    }

    public double getClose() {
        return close;
    }

    public void setClose(double close) {
        this.close = close;
    }

    public double getVolume() {
        return volume;
    }

    public void setVolume(double volume) {
        this.volume = volume;
    }
}

当我运行我的应用程序时会发生什么: 当我运行应用程序时,顶层(Stock.java)填充了一个时间序列对象,但是下一层(TimeSeries.java)不包含我期望的(Day.java)对象数组,它为空。我试过了:

@JSONProperty("2017-11-10") 
private Day day;

这确实有效,并在中间层与最后一层之间架起了一座桥梁,并且数据可以很好地通过……但这不是我所追求的。

我期望发生/想要发生的事情: 我正在尝试在天数列表中获取所有日期的数组(在 TimeSeries.java 中),但它不起作用,我相信这是因为 JSON 响应没有将它们作为 JavaScript 对象数组提供,但是而不是作为一个实际的键和对,所以我不能只列出它们,而是必须直接深入到最终对象......这不是我想要的。

我读过的关于这个主题的帖子

Parse JSON array with resttemplate

Parsing JSON with Jackson

Parsing JSON with Jackson Java

Unable to consume JSON array using Spring RestTemplate

How to use query parameter represented as JSON with Spring RestTemplate?

How to deserialize an array of objects using Jackson or any other api?

desrialize JSON with child array using Jackson annotations?

Jackson Mapping List of String or simple String

Where should I get the object to represent what is passed (Json) in a RESTful api

如果有人知道我如何使中间步骤起作用并将对象实际存储在键“时间序列(每日)”中作为天,那么访问它们的值会很棒。

注意:另外,如果您要对此投反对票,请在下面评论您这样做的原因。在过去的 5 个小时里我已经对此进行了研究,所以我相信我已经尽职尽责地能够在这里提问,并且我已经尽可能彻底......来吧。

【问题讨论】:

    标签: java arrays json spring jackson


    【解决方案1】:

    您的 json 中没有 Json 数组。你实际上有多个字段的 json 对象,所以你的 java 数据结构应该用 Map 而不是 List 表示。如果你想保持当前的类结构,那么你应该明白,你失去了时间序列的日期部分。我不认为你想要这个。


    选项 1. 使用HashMap&lt;String, Day&gt; timeSeries

    public class Stock {
    
        @JsonProperty("Time Series (Daily)")
        private HashMap<String, Day> timeSeries;
    
        public HashMap<String, Day> getTimeSeries() {
            return timeSeries;
        }
    
        public void setTimeSeries(HashMap<String, Day> timeSeries) {
            this.timeSeries = timeSeries;
        }
    }
    

    选项 2. 更改 TimeSeries 代码

    public class TimeSeries {
    
        // TreeMap because it preserves sorting order
        @JsonIgnore
        private Map<String, Day> days = new TreeMap<>();
    
        @JsonAnySetter
        public void setDays(String time, Day value){
            days.put(time,value);
        }
    
        @JsonAnyGetter
        public Map<String, Day> getData() {
            return days;
        }
    
        // add getDays() if you need only values
        // and if you need list, can use new ArrayList(days.values()) 
        @JsonIgnore
        public Collection<Day> getDays(){
            return days.values();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 2019-07-21
      • 2020-02-11
      相关资源
      最近更新 更多