2017-01-25 6 views
0

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?

Antwort

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

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

+0

Hallo shi, danke. Das macht die Arbeit. –

0

Ändern der B und C-Klassen, wie nachstehend:

Added @JsonIgnore Annotation Wert Wert gedruckt zu werden vermeiden und erstellt List<double[]> eine neue Methode getValueForJson() mit Rückgabetyp, dessen Ausgang ein tiefen geparst Ergebnis mit Werten der Klasse A. Diese neue Methode wird mit der Anmerkung @JsonProperty mit dem Wert "value" hinzugefügt, um dies als Element mit der Bezeichnung value zu behandeln.

class B extends Base { 
    @JsonIgnore 
    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; 
    } 

    @JsonProperty("value") 
    public List<double[]> getValueForJson(){ 
     List<double[]> res = new ArrayList<double[]>(); 
     value.forEach(a -> res.add(a.getValue())); 
     return res; 
    } 
} 

class C extends Base { 
    @JsonIgnore 
    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; 
    } 

    @JsonProperty("value") 
    public List<double[]> getValueForJson(){ 
     List<double[]> res = new ArrayList<double[]>(); 
     value.forEach(b -> b.getValue().forEach(a -> res.add(a.getValue()))); 
     return res; 
    } 
} 
+0

Hallo Pavan Kumar, danke, tut es das Ergebnis erhalten. Ich wollte nur fragen, ob es einen effizienteren Weg gibt? Ohne eine neue ArrayList in getValueForJson zu erstellen? –

+0

Hallo Luiz, bist du sicher über C-Ausgabe? Es ist nicht wie gewünscht, Klammerproblem. – mhshimul

+0

Ah ja, die Gruppierung ist bei C nicht gleich. Tut mir leid. –

Verwandte Themen