如何将没有密钥的原始JSON数组转换为Jackson

时间:2017-05-05 16:53:49

标签: java json jackson

我有以下Json字符串:

[512768,[1493990100000,1628.4,1628.2,1628.4,1628,7.6513558]]

我不知道如何使用Jackson将其转换为Java对象,如果没有密钥,我真的不知道怎么做。 当然我知道我需要创建一个objectMapper等...

objectMapper.readValue(candle, CandleChannel.class);

但我的意思是,我不知道应该如何注释课程。我尝试了很多东西,但我一直在收到一个错误,告诉我我无法解析它:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of application.model.CandleChannel out of START_ARRAY token
 at [Source: [512768,[1493990100000,1628.4,1628.2,1628.4,1628,7.6513558]]; line: 1, column: 1]

所以我想知道我应该如何注释课程?有人可以帮我一把吗?这是我到目前为止的代码:

public class CandleChannel {

    @JsonProperty
    private Long channelId;
    @JsonProperty 
    private List<Candle> candles;

    public CandleChannel() {
    }

    public Long getChannelId() {
        return channelId;
    }

    public void setChannelId(Long channelId) {
        this.channelId = channelId;
    }

    public List<Candle> getCandles() {
        return candles;
    }

    public void setCandles(List<Candle> candles) {
        this.candles = candles;
    }
}

这就是内在的对象:

@JsonFormat(shape= JsonFormat.Shape.ARRAY)
public class Candle {

    @JsonProperty
    private Integer mts;
    @JsonProperty
    private Float open;
    @JsonProperty
    private Float close;
    @JsonProperty
    private Integer high;
    @JsonProperty
    private Float low;
    @JsonProperty
    private Float volume;

    public Candle() {
    }

    public Integer getMts() {
        return mts;
    }

    public void setMts(Integer mts) {
        this.mts = mts;
    }

    public Float getOpen() {
        return open;
    }

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

    public Float getClose() {
        return close;
    }

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

    public Integer getHigh() {
        return high;
    }

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

    public Float getLow() {
        return low;
    }

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

    public Float getVolume() {
        return volume;
    }

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

必须有办法,但我不知道如何将其转换为Java对象。

1 个答案:

答案 0 :(得分:-1)

您的类定义需要采用以下格式的示例JSON:

{
  "CHANNEL_ID": 389251,
  "MTS": 1494001560000,
  "OPEN": 1566,
  "CLOSE": 1565.2,
  "HIGH": 1566.1,
  "LOW": 1565.1,
  "VOLUME": 126.84178235
}

看看这篇文章中接受的答案: Java: FasterXML / jackson deserialize array without keys