0

I have the following Json String:

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

I have no idea how to transform it into a Java object using Jackson, I don't really know how to do it when there are no keys. Ofcourse I know that I need to create an objectMapper etc ...

objectMapper.readValue(candle, CandleChannel.class);

But what I mean, is I have no clue how I should annotate the class. I tried many things but I just keep getting an error telling me that I can't parse it:

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]

So I just wonder how I should annotate the class? Can somebody give me a hand? This is The code I have so far:

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;
    }
}

And this the inner object:

@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;
    }
}

There must be a way, but Im not sure how to transfor that into a Java object.

javing
  • 12,307
  • 35
  • 138
  • 211
  • What do you expect `@JsonProperty("CHANNEL_ID")` to accomplish? – shmosel May 05 '17 at 17:02
  • In the online api I see that they call that field in the json channel_id You think I don't need that? – javing May 05 '17 at 17:02
  • I don't see any field names in your sample JSON. How is Jackson supposed to match them up? – shmosel May 05 '17 at 17:03
  • Yeah I think you are right maybe I don't need any of those This is the api I am using: https://bitfinex.readme.io/v2/reference#ws-public-candle – javing May 05 '17 at 17:05
  • I just removed all those field names but it also didn't work. I will update now my question with the current code – javing May 05 '17 at 17:06
  • I'm not saying removing it will fix it. I'm trying to help you see the issue. – shmosel May 05 '17 at 17:08
  • You are right actually there is a main node with an array inside that represent each candle. So I must have an outer node with a list. I just created it like in the update above. But still I get the same exception – javing May 05 '17 at 17:10
  • See also https://github.com/FasterXML/jackson-docs/wiki/Presentation-Jackson-2.1-Overview#user-content-pojo-as-array – shmosel May 05 '17 at 17:27
  • Your code just has a few minor problems: 1. You need to specify the array shape format on the `CandleChannel` class. 2. Your sample JSON has the value `1493990100000` which is out of range for the target `mts` field. 3. Your class expect a list of `Candle`, but your JSON contains no arrays other than the ones interpreted as objects. You would need a format like `[..., [[...]]]` to match your class structure. – shmosel May 07 '17 at 23:24

1 Answers1

-1

Your class definition expects a sample JSON in the format:

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

Look at the accepted answer in this post: Java: FasterXML / jackson deserialize array without keys

Community
  • 1
  • 1
toongeorges
  • 1,844
  • 17
  • 14
  • This is not a valid answer because I cannot change the Input json... There seems to be an outer object and inside an array of candles. I need to change the Java code not the input. Any idea how. Note I updated the question – javing May 05 '17 at 17:11
  • I agree. I am explaining where the error comes from. I do not know how to convert raw JSON Arrays without a key to a Java object with Jackson. I am not sure if it is possible. – toongeorges May 05 '17 at 17:14
  • Ok ... Yes exactly that is what I need. To do it without any id – javing May 05 '17 at 17:16
  • The following post suggest writing a custom deserializer: http://stackoverflow.com/questions/41719860/java-fasterxml-jackson-deserialize-array-without-keys – toongeorges May 05 '17 at 17:19
  • @toongeorges That's for invalid JSON. – shmosel May 05 '17 at 17:20
  • @shmosel look at the accepted answer of that post. It corrects the invalid JSON – toongeorges May 05 '17 at 17:24
  • Look at the answer I linked. You don't need a custom deserializer. – shmosel May 05 '17 at 17:25
  • But do you really think I need to write a custom deserializer? – javing May 05 '17 at 17:32
  • I am trying to do as that link says but the example he says tries to deserialize customly based on keys. Not sure how to make it work just for array without key – javing May 05 '17 at 17:38