This is my POJO.
这是我的POJO。
public class FolderPage {
private List<ApplicationIcon> applications;
public List<ApplicationIcon> getApplications() {
return applications;
}
public void setApplications(List<ApplicationIcon> applications) {
this.applications = applications;
}
@JsonCreator
public FolderPage(List<ApplicationIcon> applications) {
super();
this.applications = applications;
}
}
Currently it serializes as the below json { "applications": [{ "application": { "id": "com.microsoft.onenote", "name": "Micrsoft OneNote" } }, { "application": { "id": "com.microsoft.Office.word", "name": "Microsoft word" } }] }
目前它序列化为以下json {“applications”:[{“application”:{“id”:“com.microsoft.onenote”,“name”:“Micrsoft OneNote”}},{“application”:{“id “:”com.microsoft.Office.word“,”name“:”Microsoft word“}}]}
However I want to serialize it into an unnamed json array like the below
但是我想将它序列化为一个未命名的json数组,如下所示
[{
"application": {
"id": "com.microsoft.onenote",
"name": "Micrsoft OneNote"
}
}, {
"application": {
"id": "com.microsoft.Office.word",
"name": "Microsoft word"
}
}]
How can I achieve the same?
我怎样才能实现同样的目标?
EDIT POJO representing my application object
编辑POJO代表我的应用程序对象
@JsonRootName("application")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApplicationIcon extends Icon {
private String id;
private String name;
}
1 个解决方案
#1
1
Assuming ApplicationIcon
has getId()
and getName()
. You can do it with custom Serializer:
假设ApplicationIcon有getId()和getName()。您可以使用自定义Serializer执行此操作:
public static class FolderPageSearialer extends JsonSerializer<FolderPage> {
@Override
public void serialize(FolderPage o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
List<ApplicationIcon> applications = o.getApplications();
jsonGenerator.writeStartArray(applications.size());
for (ApplicationIcon application : applications) {
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("application");
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("id", application.getId());
jsonGenerator.writeStringField("name", application.getName());
jsonGenerator.writeEndObject();
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndArray();
}
}
And add @JsonSerialize on FolderPage class:
并在FolderPage类上添加@JsonSerialize:
@JsonSerialize(using = FolderPage.FolderPageSearialer.class)
public class FolderPage
#1
1
Assuming ApplicationIcon
has getId()
and getName()
. You can do it with custom Serializer:
假设ApplicationIcon有getId()和getName()。您可以使用自定义Serializer执行此操作:
public static class FolderPageSearialer extends JsonSerializer<FolderPage> {
@Override
public void serialize(FolderPage o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
List<ApplicationIcon> applications = o.getApplications();
jsonGenerator.writeStartArray(applications.size());
for (ApplicationIcon application : applications) {
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("application");
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("id", application.getId());
jsonGenerator.writeStringField("name", application.getName());
jsonGenerator.writeEndObject();
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndArray();
}
}
And add @JsonSerialize on FolderPage class:
并在FolderPage类上添加@JsonSerialize:
@JsonSerialize(using = FolderPage.FolderPageSearialer.class)
public class FolderPage