使用缺少字段的杰克逊反序列化json [英] deserializing json using jackson with missing fields

查看:112
本文介绍了使用缺少字段的杰克逊反序列化json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jackson反序列化这个JSON,我遇到了数组部分的问题,你可以看到没有字段名称。 java代码需要什么样才能反序列化?

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"
          ]
       ]
    }

谢谢,

bc

推荐答案

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

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

使用:

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.

这篇关于使用缺少字段的杰克逊反序列化json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆