Я использую ObjectMapper, чтобы сделать свое отображение java-json.Порядок объектов JSON с использованием ObjectMapper от Jackson
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
ow.writeValue(new File(fileName +".json"), jsonObj);
это мой Java класс:
public class Relation {
private String id;
private String source;
private String target;
private String label;
private List<RelAttribute> attributes;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public void setAttributes(List<RelAttribute> attributes) {
this.attributes = attributes;
}
public List<RelAttribute> getAttributes() {
return attributes;
}
}
это то, что я получаю:
{
"id" : "-75da69d3-79c8-4000-a3d8-b10350a57a7e",
"attributes" : [ {
"attrName" : "ID",
"attrValue" : ""
}, {
"attrName" : "Description",
"attrValue" : "Primary Actor"
}, {
"attrName" : "Status",
"attrValue" : ""
} ],
"label" : "new Label",
"target" : "-46b238ac-b8b3-4230-b32c-be9707f8b691",
"source" : "-daa34638-061a-45e0-9f2e-35afd6c271e0"
}
Так что мой вопрос теперь, как я могу получить этот выход JSon:
{
"id" : "-75da69d3-79c8-4000-a3d8-b10350a57a7e",
"label" : "new Label",
"target" : "-46b238ac-b8b3-4230-b32c-be9707f8b691",
"source" : "-daa34638-061a-45e0-9f2e-35afd6c271e0",
"attributes" : [ {
"attrName" : "ID",
"attrValue" : ""
}, {
"attrName" : "Description",
"attrValue" : "Primary Actor"
}, {
"attrName" : "Status",
"attrValue" : ""
} ]
}
Я хочу, чтобы он был в том же порядке, что и в моей декларации java. Есть ли способ указать его? Может быть, с аннотациями или такими вещами?
Собственный заказ, имеющий важное значение для запаха кода/дизайна. Все, что потребляет JSON, не должно заботиться о заказе (списки да, свойства нет). – ach
Мне просто нужно это для удобочитаемости :-) – justSaid
@ach это просто неправда. Закаленная карта - очень распространенный и очень полезный тип данных. Желательный порядок вовсе не запах. – goat