使用Jackson的ObjectMapper的JSON对象的顺序

jus*_*aid 78 java json jackson

我正在使用ObjectMapper来做我的java-json映射.

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
ow.writeValue(new File( fileName +".json"), jsonObj);
Run Code Online (Sandbox Code Playgroud)

这是我的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;
}

}
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

{
    "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"
  }
Run Code Online (Sandbox Code Playgroud)

所以现在我的问题是,如何获得这个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" : ""
    } ]

  }
Run Code Online (Sandbox Code Playgroud)

我希望它与我的java声明中的顺序相同.有没有办法指定它?也许有注释或类似的东西?

And*_*pin 170

@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" })
public class Relation { ... }
Run Code Online (Sandbox Code Playgroud)


小智 21

生成的 .class 中字段的顺序是不确定的,因此您不能指望这一点。

如果您想要每个类的特定顺序,那么您需要使用其他答案中指定的方法之一。

如果您希望所有内容都默认按字母顺序排序(例如,为了保持 JSON 结构的一致性),那么您可以像这样配置 ObjectMapper:

ObjectMapper mapper = new ObjectMapper();
mapper.setConfig(mapper.getSerializationConfig()
   .with(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
Run Code Online (Sandbox Code Playgroud)

为了获得更一致的 JSON,还可以考虑添加:

   .with(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
Run Code Online (Sandbox Code Playgroud)

这种方法的优点之一是您不必修改每个正在序列化的类。


Ryt*_*mic 14

我今天发现了第三种方法,以防字母顺序不是您想要的排序顺序。事实证明,在字段上添加 @JsonProperty 注释会将其放在写入时的最后。我发现当我想指定一个不符合java命名约定的属性名称时。

通过添加索引属性,您可以定义顺序。最低的索引放在最前面。

@JsonProperty(index=20)
String prop1;

@JsonProperty(index=10)
String prop2;
Run Code Online (Sandbox Code Playgroud)

将呈现:

{"prop2": "valueProp2", "prop1": "valueProp1"}
Run Code Online (Sandbox Code Playgroud)

  • 最佳解决方案,除了仅当“index != -1”(默认值)时“最低索引放置在前面”为真 (2认同)

naX*_*aXa 11

您知道有一种方便的方法可以指定字母顺序吗?

@JsonPropertyOrder(alphabetic = true)
public class Relation { ... }
Run Code Online (Sandbox Code Playgroud)

如果您有特定要求,请在此处配置自定义排序:

@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" })
public class Relation { ... }
Run Code Online (Sandbox Code Playgroud)