Spring RestTemplate杰克逊反序列化数组

时间:2018-02-10 03:58:02

标签: 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。

我想将这些嵌套的数组元素映射到烛台对象。以下是我的烛台课程。

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对象?

2 个答案:

答案 0 :(得分:3)

您需要执行以下操作;

  1. 为烛台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);
    

答案 1 :(得分:1)

设计自定义反序列化器是这个问题的合理解决方案。但是将数组映射到对象似乎是一项棘手的工作。将@ shazin的想法保持为基础,将数组数组映射到对象列表的自定义反序列化器可以写成如下:

public class CStickListDeserializer extends StdDeserializer<ArrayList<Candlestick>> { 

    public CStickListDeserializer() { 
        this(null); 
    } 

    public CStickListDeserializer(Class<?> c) { 
        super(c); 
    }

    @Override
    public ArrayList<Candlestick> deserialize(JsonParser jp, DeserializationContext ctxt) 
      throws IOException, JsonProcessingException {

        Candlestick cs ;
        ArrayList<Candlestick> cList = new ArrayList<Candlestick>();
        BigDecimal[][] a = jp.readValueAs(BigDecimal[][].class);
        for(BigDecimal[] a1 : a){
            cs = new Candlestick();
            cs.setTime(a1[0].longValue());
            cs.setLow(a1[1]);
            cs.setHigh(a1[2]);
            cs.setOpen(a1[3]);
            cs.setClose(a1[4]);
            cs.setVolume(a1[5]); 
            cList.add(cs);
        }
             return cList;
    }
}

<强>用法:

     ObjectMapper om = new ObjectMapper();
     SimpleModule mod = new SimpleModule();
     mod.addDeserializer(ArrayList.class, new CStickListDeserializer());
     om.registerModule(mod);
     ArrayList<Candlestick> CandlestickList = om.readValue(json, ArrayList.class);