如何使用 Jackson 更改 JSON 中的字段名称

发布于 2024-12-02 12:13:29 字数 568 浏览 1 评论 0原文

我正在使用 Jackson 将我的对象转换为 json。 该对象有 2 个字段:

@Entity
public class City {
   @id
   Long id;
   String name;
   public String getName() { return name; }
   public void setName(String name){ this.name = name; }
   public Long getId() { return id; }
   public void setName(Long id){ this.id = id; }
}

因为我想将其与 jQuery 自动完成功能一起使用,所以我希望“id”在 json 中显示为“值”,“名称”显示为“标签”。 Jackson 的文档对此并不清楚,我已经尝试了所有注释,即使在远程看来它也能满足我的需要,但我无法让 name 显示为 labelid 在 json 中显示为 value

有谁知道如何做到这一点或者这是否可能?

I'm using jackson to convert an object of mine to json.
The object has 2 fields:

@Entity
public class City {
   @id
   Long id;
   String name;
   public String getName() { return name; }
   public void setName(String name){ this.name = name; }
   public Long getId() { return id; }
   public void setName(Long id){ this.id = id; }
}

Since I want to use this with the jQuery auto complete feature I want 'id' to appear as 'value' in the json and 'name' to appear as 'label'. The documentation of jackson is not clear on this and I've tried every annotation that even remotely seems like it does what I need but I can't get name to appear as label and id to appear as value in the json.

Does anyone know how to do this or if this is possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昵称有卵用 2024-12-09 12:13:29

您是否尝试过使用@JsonProperty?

@Entity
public class City {
   @id
   Long id;
   String name;

   @JsonProperty("label")
   public String getName() { return name; }

   public void setName(String name){ this.name = name; }

   @JsonProperty("value")
   public Long getId() { return id; }

   public void setId(Long id){ this.id = id; }
}

Have you tried using @JsonProperty?

@Entity
public class City {
   @id
   Long id;
   String name;

   @JsonProperty("label")
   public String getName() { return name; }

   public void setName(String name){ this.name = name; }

   @JsonProperty("value")
   public Long getId() { return id; }

   public void setId(Long id){ this.id = id; }
}
小嗷兮 2024-12-09 12:13:29

请注意,Jackson 1.x 中有 org.codehaus.jackson.annotate.JsonProperty ,Jackson 2.x 中有 com.fasterxml.jackson.annotation.JsonProperty 。检查您正在使用哪个 ObjectMapper(来自哪个版本),并确保使用正确的注释。

Be aware that there is org.codehaus.jackson.annotate.JsonProperty in Jackson 1.x and com.fasterxml.jackson.annotation.JsonProperty in Jackson 2.x. Check which ObjectMapper you are using (from which version), and make sure you use the proper annotation.

半透明的墙 2024-12-09 12:13:29

Jackson

如果您使用 Jackson,那么您可以使用 @JsonProperty 注释用于自定义给定 JSON 属性的名称。

因此,您只需使用 @JsonProperty 注解对实体字段进行注解并提供自定义 JSON 属性名称,如下所示:

@Entity
public class City {

   @Id
   @JsonProperty("value")
   private Long id;

   @JsonProperty("label")
   private String name;

   //Getters and setters omitted for brevity
}

JavaEE 或 JakartaEE JSON-B

JSON-B 是用于转换 Java 的标准绑定层对象与 JSON 之间的转换。如果您使用的是 JSON-B,则可以通过 < 覆盖 JSON 属性名称code>@JsonbProperty 注解:

@Entity
public class City {

   @Id
   @JsonbProperty("value")
   private Long id;

   @JsonbProperty("label")
   private String name;

   //Getters and setters omitted for brevity
}

Jackson

If you are using Jackson, then you can use the @JsonProperty annotation to customize the name of a given JSON property.

Therefore, you just have to annotate the entity fields with the @JsonProperty annotation and provide a custom JSON property name, like this:

@Entity
public class City {

   @Id
   @JsonProperty("value")
   private Long id;

   @JsonProperty("label")
   private String name;

   //Getters and setters omitted for brevity
}

JavaEE or JakartaEE JSON-B

JSON-B is the standard binding layer for converting Java objects to and from JSON. If you are using JSON-B, then you can override the JSON property name via the @JsonbProperty annotation:

@Entity
public class City {

   @Id
   @JsonbProperty("value")
   private Long id;

   @JsonbProperty("label")
   private String name;

   //Getters and setters omitted for brevity
}
小…楫夜泊 2024-12-09 12:13:29

还有一个选项可以重命名字段:

Jackson MixIns

如果您处理第三方类,而您无法对其进行注释,或者您只是不想用 Jackson 特定的注释来污染该类,那么这很有用。

Mixins 的 Jackson 文档已经过时,因此这个 示例 可以提供更清晰的信息。本质上:您创建 mixin 类,它以您想要的方式进行序列化。然后将其注册到ObjectMapper:

objectMapper.addMixIn(ThirdParty.class, MyMixIn.class);

There is one more option to rename field:

Jackson MixIns.

Useful if you deal with third party classes, which you are not able to annotate, or you just do not want to pollute the class with Jackson specific annotations.

The Jackson documentation for Mixins is outdated, so this example can provide more clarity. In essence: you create mixin class which does the serialization in the way you want. Then register it to the ObjectMapper:

objectMapper.addMixIn(ThirdParty.class, MyMixIn.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文