user1644514
user1644514

Reputation: 31

deserializing json using jackson with missing fields

I am trying to deserialize this JSON using Jackson and I'm having trouble with the array part which as you can see has no field names. What would the java code need to look like to deserialize this?

   {
       "foo":[
          [
             "11.25",
             "0.88"
          ],
          [
             "11.49",
             "0.78976802"
          ]
       ],
       "bar":[
          [
             "10.0",
             "0.869"
          ],
          [
             "9.544503",
             "0.00546545"
          ],
          [
             "9.5",
             "0.14146579"
          ]
       ]
    }

Thanks,

bc

Upvotes: 3

Views: 1167

Answers (1)

Mark Peters
Mark Peters

Reputation: 81154

The closest mapping (without any more context) would be to make foo and bar each an array of double arrays (2-dimensional arrays).

public class FooBarContainer {

    private final double[][] foo;
    private final double[][] bar;

    @JsonCreator
    public FooBarContainer(@JsonProperty("foo") double[][] foo, @JsonProperty("bar") double[][] bar) {
        this.bar = bar;
        this.foo = foo;
    }
}

To use:

public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    FooBarContainer fooBarContainer = mapper.readValue(CONTENT, FooBarContainer.class);

    //note: bar is visible only if main is in same class
    System.out.println(fooBarContainer.bar[2][1]); //0.14146579
}

Jackson has no trouble deserializing that data into this class.

Upvotes: 1

Related Questions