Java 使用 Jackson 的 ObjectMapper 的 JSON 对象顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19272830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Order of JSON objects using Hymanson's ObjectMapper
提问by justSaid
I'm using ObjectMapperto do my java-json mapping.
我正在使用ObjectMapper来做我的 java-json 映射。
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
ow.writeValue(new File( fileName +".json"), jsonObj);
this is my java class:
这是我的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;
}
}
this is what I get:
这就是我得到的:
{
"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"
}
So my question now is, how can I get this json output:
所以我现在的问题是,我怎样才能得到这个 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" : ""
} ]
}
I want it with same order as in my java declaration. Is there a way to specify it ? Maybe with annotations or stuff like that ?
我希望它的顺序与我的 java 声明中的顺序相同。有没有办法指定它?也许带有注释或类似的东西?
采纳答案by Andrey Atapin
@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" })
public class Relation { ... }
回答by user1001
You can use @XmlAccessorType(XmlAccessType.FIELD)
您可以使用 @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "response", propOrder = { "prop1", "prop2",
"prop3", "prop4", "prop5", "prop6" }).
@JsonPropertyOrder
requires a new jar to be added.
@JsonPropertyOrder
需要添加一个新的 jar。
回答by naXa
Do you know there is a convenient way to specify alphabetic ordering?
您知道有一种方便的方法可以指定字母顺序吗?
@JsonPropertyOrder(alphabetic = true)
public class Relation { ... }
If you have specific requirements, here how you configure custom ordering:
如果您有特定要求,请在此处配置自定义排序:
@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" })
public class Relation { ... }