简体   繁体   English

使用缺少字段的杰克逊反序列化json

[英]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. 我正在尝试使用Jackson来反序列化此JSON,而数组部分却遇到了麻烦,如您所见,它没有字段名。 What would the java code need to look like to deserialize this? 反序列化该Java代码将需要什么样的代码?

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

Thanks, 谢谢,

bc 公元前

The closest mapping (without any more context) would be to make foo and bar each an array of double arrays (2-dimensional arrays). 最接近的映射(没有更多上下文)将使foobar成为双精度数组(二维数组)的数组。

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. Jackson毫不费力地将该数据反序列化为此类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM