首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用杰克逊的ObjectMapper的JSON对象的顺序

使用杰克逊的ObjectMapper的JSON对象的顺序
EN

Stack Overflow用户
提问于 2013-10-09 21:01:35
回答 6查看 72.1K关注 0票数 100

我正在使用ObjectMapper进行java-json映射。

代码语言:javascript
复制
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
ow.writeValue(new File( fileName +".json"), jsonObj);

这是我的java类:

代码语言:javascript
复制
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;
}

}

这是我得到的:

代码语言:javascript
复制
{
    "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输出:

代码语言:javascript
复制
{
    "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声明中的顺序相同。有没有一种方法来指定它?也许用注解或类似的东西?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-10-09 21:05:50

代码语言:javascript
复制
@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" })
public class Relation { ... }
票数 210
EN

Stack Overflow用户

发布于 2020-02-28 23:10:39

你知道有一种方便的方法来指定字母顺序吗?

代码语言:javascript
复制
@JsonPropertyOrder(alphabetic = true)
public class Relation { ... }

如果您有特定的需求,下面是如何配置自定义排序的:

代码语言:javascript
复制
@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" })
public class Relation { ... }
票数 11
EN

Stack Overflow用户

发布于 2021-04-10 01:05:59

在生成的.class中,字段的排序是不确定的,因此您不能指望它。

如果您希望按类进行特定的排序,则需要使用其他答案中指定的方法之一。

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

代码语言:javascript
复制
ObjectMapper mapper = new ObjectMapper();
mapper.setConfig(mapper.getSerializationConfig()
   .with(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));

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

代码语言:javascript
复制
   .with(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19272830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档