使用Jackson將POJO類轉換爲JSON。如何編輯JSON格式
String jsonInString = mapper.writeValueAsString(Table 1);
我已經JSON像下面
{
"id": #,
"name":"name",
"Table 2":[
{
"id": #,
"name":"name",
"Table 3":[
{
"id": #,
"name":"name"
}
],
"Table 4":[
{
"id": #,
"name":"name"
}
]
},
{
"id": #,
"name":"name"
}
]
}
但我想JSON像
{
"TableName":"Table 1"
"id": #,
"name":"name",
"children":[
{
"TableName":"Table 2"
"id": #,
"name":"name",
"children":[
{
"TableName":"Table 3"
"id": #,
"name":"name"
},
{
"TableName":"Table 4"
"id": #,
"name":"name"
}
]
},
{
"TableName":"Table 2"
"id": #,
"name":"name"
}
]
}
我怎麼可以編輯JSON格式也是這樣嗎? 在此先感謝..
我的POJO類的結構是這樣
Table 1.java
@JsonProperty("children")
private Set<Table2> Table2 = new HashSet<Table2>(0);
Table 2.java
@JsonProperty("children")
private Set<Table1> Table1 = new HashSet<Table1>(0);
//@JsonProperty("children")
private Set<Table3> Table3 = new HashSet<Table3>(0);
//@JsonProperty("children")
private Set<Table4> Table4 = new HashSet<Table4>(0);
Table 3.java
@JsonProperty("children")
private Set<Table2> Table2 = new HashSet<Table2>(0);
Table 4.java
@JsonProperty("children")
private Set<Table2> Table2 = new HashSet<Table2>(0);
在表2.java,我想要孩子下表3 &表4。 但我無法使用@JsonProperty(「children」)爲表3 &表4,它們將在名稱之間發生衝突。
"children":[
{
"TableName":"Table 3"
"id": #,
"name":"name"
},
{
"TableName":"Table 4"
"id": #,
"name":"name"
}
]
我們如何在這裏創建Table 2.java POJO類?
您還可以分享pojo嗎? JSON將總是依賴於對象的結構。你不能簡單地改變json –
這可能會幫助你http://www.jsonschema2pojo.org/ –