2

我正在尝试使用 Jackson 反序列化此 JSON,但我遇到了数组部分的问题,如您所见,它没有字段名称。java代码需要什么样的反序列化?

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

谢谢,

公元前

4

1 回答 1

1

foo最接近的映射(没有更多上下文)将是bar每个双数组(二维数组)的数组。

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

要使用:

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 可以轻松地将这些数据反序列化到此类中。

于 2012-10-03T16:46:34.113 回答