[英]Convert a part of Json to HashMap using Jackson ObjectMapper
[英]Order of JSON objects using Jackson's ObjectMapper
我正在使用ObjectMapper來做我的 java-json 映射。
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 聲明中的順序相同。 有沒有辦法指定它? 也許帶有注釋或類似的東西?
@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" })
public class Relation { ... }
您知道有一種方便的方法可以指定字母順序嗎?
@JsonPropertyOrder(alphabetic = true)
public class Relation { ... }
如果您有特定要求,請在此處配置自定義排序:
@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" })
public class Relation { ... }
生成的 .class 中字段的順序是不確定的,因此您不能指望這一點。
如果您想要每個班級的特定排序,那么您需要使用其他答案中指定的方法之一。
如果您希望所有內容都默認為按字母順序排列(例如,為了 JSON 結構的一致性),那么您可以像這樣配置 ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
mapper.setConfig(mapper.getSerializationConfig()
.with(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
要獲得更一致的 JSON,還可以考慮添加:
.with(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
這種方法的一個優點是您不必修改每個被序列化的類。
我今天發現了第三種方法,以防字母不是您想要的排序順序。 事實證明,在字段上添加 @JsonProperty 注釋會將其放在最后寫入時。 我發現當我想指定一個不符合 java 命名約定的屬性名稱時。
通過添加索引屬性,您可以定義順序。 最低的索引放在最前面。
@JsonProperty(index=20)
String prop1;
@JsonProperty(index=10)
String prop2;
將呈現:
{"prop2": "valueProp2", "prop1": "valueProp1"}
你可以使用@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "response", propOrder = { "prop1", "prop2",
"prop3", "prop4", "prop5", "prop6" }).
@JsonPropertyOrder
需要添加一個新的jar。
根據本文檔,您可以全局配置 Jackson2ObjectMapperBuilder。 此類在 spring-web 依賴項中可用。
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToEnable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
return builder;
}
此外,您還可以使用 @JsonProperty(index) 來確定繼承類中的順序。
class animal {
@JsonProperty(index=2)
int p1;
@JsonProperty(index=3)
int p2;
}
class cat extends animal{
@JsonProperty(index=1)
int p3;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.