java - Spring RestTemplate Jackson 反序列化数组数组

标签 java json spring jackson resttemplate

我使用 Spring RestTemplate 从 API 中提取数据,该 API 返回以下格式的未命名数组:

[
    [
        1518230040,
        8863.96,
        8876.95,
        8863.96,
        8876.94,
        1.81986441
    ],
    [
        1518229980,
        8851.03,
        8862.53,
        8853.79,
        8862.53,
        2.95533163
    ],
    [
        1518229920,
        8853.79,
        8855.3,
        8855.29,
        8853.8,
        3.0015851299999996
    ]
]

嵌套数组中的索引代表 Candlestick 的以下属性:

0 => time
1 => low
2 => high
3 => open
4 => close
5 => volume

例如,在第一个嵌套数组中,蜡烛图的时间为 1518230040,最低价为 8863.96,最高价为 8876.95 ,开盘价为 8863.96,收盘价为 8876.94,成交量为 1.81986441。

我想将这些嵌套数组元素映射到 Candlestick 对象。下面是我的 Candlestick 类。

import java.math.BigDecimal;

public class Candlestick {
    private long time;

    private BigDecimal low;

    private BigDecimal high;

    private BigDecimal open;

    private BigDecimal close;

    private BigDecimal volume;

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public BigDecimal getLow() {
        return low;
    }

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

    public BigDecimal getHigh() {
        return high;
    }

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

    public BigDecimal getOpen() {
        return open;
    }

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

    public BigDecimal getClose() {
        return close;
    }

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

    public BigDecimal getVolume() {
        return volume;
    }

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

    @Override
    public String toString() {
        return "Candle [time=" + time + ", low=" + low + ", high=" + high + ", open=" + open + ", close=" + close
                + ", volume=" + volume + "]";
    }
}

如何使用 Spring RestTemplate 和 Jackson 将嵌套数组映射到 Candlestick 对象?

最佳答案

您需要执行以下操作;

  1. 为 Candlestick pojo 配置编写自定义反序列化器

    public class CandlestickDeserializer extends StdDeserializer<Candlestick> { 
    
        public CandlestickDeserializer() { 
            this(null); 
        } 
    
        public CandlestickDeserializer(Class<?> vc) { 
            super(vc); 
        }
    
        @Override
        public Item deserialize(JsonParser jp, DeserializationContext ctxt) 
          throws IOException, JsonProcessingException {
            JsonNode node = jp.getCodec().readTree(jp);
            // Read the Array or Arrays
    
            return new Candlestick(// Return with correct values read from Array);
        }
    }
    
  2. ObjectMapper 使用自定义反序列化器

    // This will get the Spring ObjectMapper singleton instance.
    @Autowired
    ObjectMapper mapper;
    
    @PostConstruct
    public void init() throws Exception {
        SimpleModule module = new SimpleModule();
        module.addDeserializer(Candlestick.class, new CandlestickDeserializer());
        mapper.registerModule(module);
    }     
    
    
    Candlestick readValue = mapper.readValue(json, Candlestick.class);
    

关于java - Spring RestTemplate Jackson 反序列化数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48717071/

相关文章:

java - 确保方法只能由选定的方法执行

java - 如何将复选框绑定(bind)到 Play 中的 boolean 值!框架

java - 为什么请求会发送到 Struts Dispatcher?

sql - 从 JSONB 字段中正确提取 JSON 数组

java - 如何使restful端点仅可供特定用户访问?

java - 将@Transactional 与 JOOQ 一起使用

java - 从一对一关系 hibernate 中的表中删除

java - 从 Android 应用程序发送 OSC(开放式声音控制)消息

java - Asynctask创建问题

json - Flutter:为 Http GET 请求发送 JSON 正文