반응형
Jackson의 ObjectMapper를 사용한 JSON 객체 순서
ObjectMapper 를 사용하여 Java-json 매핑을 수행하고 있습니다.
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
ow.writeValue(new File( fileName +".json"), jsonObj);
이것은 내 자바 클래스입니다.
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" : ""
} ]
}
내 자바 선언과 동일한 순서로 원합니다. 그것을 지정하는 방법이 있습니까? 주석이나 그와 같은 것들로?
@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" })
public class Relation { ... }
당신이 사용할 수있는 @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "response", propOrder = { "prop1", "prop2",
"prop3", "prop4", "prop5", "prop6" }).
@JsonPropertyOrder
추가하려면 새 항아리가 필요합니다.
참고 URL : https://stackoverflow.com/questions/19272830/order-of-json-objects-using-jacksons-objectmapper
반응형
'Program Tip' 카테고리의 다른 글
사용자 정의보기가있는 UIBarButtonItem이 왼쪽 또는 오른쪽 탐색 모음 항목으로 사용될 때 iOS 7에서 제대로 정렬되지 않음 (0) | 2020.10.18 |
---|---|
gdb의 주어진 주소에서 어셈블리 명령을 중단하는 방법은 무엇입니까? (0) | 2020.10.18 |
SQL Server에서 두 날짜 간의 시간 (십진수) 차이를 계산하는 방법은 무엇입니까? (0) | 2020.10.18 |
LINQ Lambda 식에서 GroupBy, Count 및 Sum 사용 (0) | 2020.10.18 |
sqlite에서 int를 실수로 변환 (0) | 2020.10.18 |