我有以下课程:
public class Rows {
@JacksonXmlElementWrapper(useWrapping = false)
@JsonProperty(value = "row")
@JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.NAME)
private List<List<Column>> data;
}
这是我的专栏课。
@JacksonXmlRootElement(localName = "column")
public class Column {
@JacksonXmlProperty(isAttribute = true)
@JsonProperty("@name")
private String name;
@JsonDeserialize(using = JavaTypeDeserializer.class)
@JacksonXmlProperty(isAttribute = true, localName = "class")
@JsonProperty(value = "@class")
private Class<?> classType;
@JacksonXmlProperty(isAttribute = true)
@JsonProperty("@type")
private String type;
@JacksonXmlText
@JsonProperty("$")
private String content;
现在,我想在JSON中获得此结果:
"rows": {
"row": [
{
"column": [
{
"@name": "name",
"@type": "type",
"$": "123"
},
{
"@name": "name",
"@type": "type",
"$": "1234"
},
{
"@name": "name",
"@type": "type",
"$": "1243"
}
]
}
]
}
当我执行上面的代码时,我几乎得到了我想要的结果,但是周围的对象“ column”被称为“ ArrayList”。
是否可以为此对象配置名称? 谢谢!
答案 0 :(得分:1)
这有点不合常规,但这会很好吗?
class Container {
private Rows rows;
public Rows getRows() {
return rows;
}
public void setRows(Rows rows) {
this.rows = rows;
}
}
class Rows {
@JsonProperty("row")
private List<Row> rows;
public List<Row> getRows() {
return rows;
}
public void setRows(List<Row> rows) {
this.rows = rows;
}
}
class Row {
@JsonProperty("column")
private List<Column> columns;
public void setColumns(List<Column> columns) {
this.columns = columns;
}
public List<Column> getColumns() {
return columns;
}
}
class Column {
@JacksonXmlProperty(isAttribute = true)
@JsonProperty("@name")
private String name;
@JsonDeserialize(using = JavaTypeDeserializer.class)
@JacksonXmlProperty(isAttribute = true, localName = "class")
@JsonProperty(value = "@class")
private Class<?> classType;
@JacksonXmlProperty(isAttribute = true)
@JsonProperty("@type")
private String type;
@JacksonXmlText
@JsonProperty("$")
private String content;
public void setName(String name) {
this.name = name;
}
public void setClassType(Class<?> classType) {
this.classType = classType;
}
public void setType(String type) {
this.type = type;
}
public void setContent(String content) {
this.content = content;
}
}
当我序列化一个容器时,会产生:
{
"rows" : {
"row" : [ {
"column" : [ {
"@name" : "1552663236010",
"@class" : null,
"@type" : null,
"$" : null
}, {
"@name" : "1552663236010",
"@class" : null,
"@type" : null,
"$" : null
} ]
}, {
"column" : [ {
"@name" : "1552663236010",
"@class" : null,
"@type" : null,
"$" : null
}, {
"@name" : "1552663236010",
"@class" : null,
"@type" : null,
"$" : null
} ]
} ]
}
}