Guten tag alle, Ich möchte eine eigenschaft und eine eigenschaft name eines objekts, das ist in einer Liste <> wenn von einem anderen objekt (als eigenschaft) zurückgegeben .jackson json serialisieren spezifische eigenschaft wert der liste der liste
Ich habe die folgenden Klassen:
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;
}
}
Und sie mögen verwenden:
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();
}
Wo erhalte ich:
=== 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 ]
} ]
} ]
}
Aber ich will ihn haben:
=== 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 ] ] ]
}
konnte ich die Eigenschaft „type“ von A, wenn sie von B und Eigenschaft „type“ von B aus C unter Verwendung zurückgegeben entfernen:
@JsonIgnoreProperties({ "type" })
public List<A> getValue() {
return value;
}
...
@JsonIgnoreProperties({ "type" })
public List<B> getValue() {
return value;
}
Aber ich bin noch immer die Eigenschaft „Wert“ Namen und die geschweiften Klammern ...
=== 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 ]
} ]
} ]
}
Welche Anmerkung kann ich die gewünschte Ausgabe erhalten? Oder muss ich meinen Unterricht neu gestalten?
Hi shi, danke, sieht vielversprechend aus, aber der "Wert" von B und C wird zur Zeichenkette (eingeschlossen in "") "type": "B", "Wert": "[[3.0, 4.0], [5.0 , 6,0]]“ } { "type": "C", "Wert": "[[[7,0, 8,0], [9,0, 10,0]], [[11.0, 12.0]]]" } –
Hallo shi, danke. Das macht die Arbeit. –