deserializing json using jackson with missing fields

主宰稳场 提交于 2019-12-11 03:49:36

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/12253233/deserializing-json-using-jackson-with-missing-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!