jackson json序列化list-of-list

时间:2017-01-25 04:02:33

标签: java json jackson

大家好日子, 我想删除List<>中的对象的属性和属性名称。从另一个对象(作为属性)返回时。

我有以下课程:

public class Base {
    protected String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

public class A extends Base {
    private double[] value;

    public A() { type = "A"; }

    public A(double[] value) {
        this();
        setValue(value);
    }

    public double[] getValue() {
        return value;
    }

    public void setValue(double[] value) {
        this.value = value;
    }
}

public class B extends Base {
    private List<A> value = new ArrayList<A>();

    public B() { type = "B"; }

    public B(List<A> value) {
        this();
        setValue(value);
    }

    public List<A> getValue() { return value; }

    public void setValue(List<A> value) {
        this.value = value;
    }
}

public class C extends Base {
    private List<B> value = new ArrayList<B>();

    public C() { type = "C"; }

    public List<B> getValue() {
        return value;
    }

    public void setValue(List<B> value) {
        this.value = value;
    }
}

并使用它们:

A a = new A();
a.setValue(new double[]{1,2});

B b = new B();
List<A> aList = new ArrayList<A>();
aList.add(new A(new double[]{1,2}));
aList.add(new A(new double[]{3,4}));
b.setValue(aList);

C c = new C();
List<B> bList = new ArrayList<B>();
List<A> aList1 = new ArrayList<A>();
List<A> aList2 = new ArrayList<A>();
aList1.add(new A(new double[]{1,2}));
aList1.add(new A(new double[]{3,4}));
aList2.add(new A(new double[]{5,6}));
bList.add(new B(aList1));
bList.add(new B(aList2));
c.setValue(bList);

ObjectMapper jsonMapper = new ObjectMapper();
jsonMapper.enable(SerializationFeature.INDENT_OUTPUT);

try {
    System.out.println("=== com.example.A ===");
    System.out.println(jsonMapper.writeValueAsString(a));

    System.out.println("=== com.example.B ===");
    System.out.println(jsonMapper.writeValueAsString(b));

    System.out.println("=== com.example.C ===");
    System.out.println(jsonMapper.writeValueAsString(c));
} catch (JsonProcessingException e) {
    e.printStackTrace();
}

我得到的地方:

=== com.example.A ===
{
  "type" : "A",
  "value" : [ 1.0, 2.0 ]
}
=== com.example.B ===
{
  "type" : "B",
  "value" : [ {
    "type" : "A",
    "value" : [ 1.0, 2.0 ]
  }, {
    "type" : "A",
    "value" : [ 3.0, 4.0 ]
  } ]
}
=== com.example.C ===
{
  "type" : "C",
  "value" : [ {
    "type" : "B",
    "value" : [ {
      "type" : "A",
      "value" : [ 1.0, 2.0 ]
    }, {
      "type" : "A",
      "value" : [ 3.0, 4.0 ]
    } ]
  }, {
    "type" : "B",
    "value" : [ {
      "type" : "A",
      "value" : [ 5.0, 6.0 ]
    } ]
  } ]
}

但我希望如此:

=== com.example.A ===
{
  "type" : "A",
  "value" : [ 1.0, 2.0 ]
}
=== com.example.B ===
{
  "type" : "B",
  "value" : [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
}
=== com.example.C ===
{
  "type" : "C",
  "value" : [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ]
}

我能够删除该属性&#34;输入&#34;当从B和财产&#34;类型&#34;返回时A来自C的B使用:

    @JsonIgnoreProperties( { "type" })
    public List<A> getValue() {
        return value;
    }
...
    @JsonIgnoreProperties( { "type" })
    public List<B> getValue() {
        return value;
    }

但我仍然得到了财产&#34;价值&#34;名字和花括号......

=== com.example.A ===
{
  "type" : "A",
  "value" : [ 1.0, 2.0 ]
}
=== com.example.B ===
{
  "type" : "B",
  "value" : [ {
    "value" : [ 1.0, 2.0 ]
  }, {
    "value" : [ 3.0, 4.0 ]
  } ]
}
=== com.example.C ===
{
  "type" : "C",
  "value" : [ {
    "value" : [ {
      "value" : [ 1.0, 2.0 ]
    }, {
      "value" : [ 3.0, 4.0 ]
    } ]
  }, {
    "value" : [ {
      "value" : [ 5.0, 6.0 ]
    } ]
  } ]
}

我可以使用什么注释来获得所需的输出?或者我是否需要重新设计课程?

2 个答案:

答案 0 :(得分:0)

修改B和C类,如下所示:

为值添加@JsonIgnore注释以避免打印值并创建一个返回类型为getValueForJson()的新方法List<double[]>,其输出为深度解析结果,其值为{{1} }。这个新方法添加了注释A,其值为@JsonProperty,以便将其视为名为"value"的成员。

value

答案 1 :(得分:0)

public class A extends Base {
    private double[] value;

    public A() {
        type = "A";
    }

    public A(double[] value) {
        this();
        setValue(value);
    }

    public double[] getValue() {
        return value;
    }

    public void setValue(double[] value) {
        this.value = value;
    }
}

public class B extends Base {

    private List<A> value = new ArrayList<A>();

    public B() {
        type = "B";
    }

    public B(List<A> value) {
        this();
        setValue(value);
    }

    @JsonIgnoreProperties({"type"})
    public List<A> getValue() {
        return value;
    }

    @JsonGetter("value")
    @JsonIgnoreProperties({"type"})
    public List<double[]> getA() {
        List<double[]> res = new ArrayList<>();
        for (A a : value) {
            res.add(a.getValue());
        }
        return res;
    }

    public void setValue(List<A> value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "" + value;
    }
}

public class C extends Base {
    private List<B> value = new ArrayList<B>();

    public C() {
        type = "C";
    }

    @JsonGetter("value")
    @JsonIgnoreProperties({"type"})
    public List<List<double[]>> getB() {
        List<List<double[]>> res1 = new ArrayList<>();            
        for (B b : value) {
            List<double[]> res2 = new ArrayList<>();
            for (A a : b.getValue()) {
                res2.add(a.getValue());
            }
            res1.add(res2);
        }
        return res1;
    }

    public List<B> getValue() {
        return value;
    }

    public void setValue(List<B> value) {
        this.value = value;
    }
}