使用Jackson将POJO类转换为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中,我想要Table3&amp;儿童表4。 但我不能将@JsonProperty(“children”)用于Table3&amp;表4,它们之间会发生冲突。
"children":[
{
"TableName":"Table 3"
"id": #,
"name":"name"
},
{
"TableName":"Table 4"
"id": #,
"name":"name"
}
]
我们如何在这里创建Table 2.java POJO类?
答案 0 :(得分:0)
创建正确的POJO可以解决您的问题。您可以创建如下所示的POJO
Class Table{
String tableName;
Long id;
String name;
List<Table> childrens;
}