如何使用 Jackson 更改 JSON 中的字段名称
我正在使用 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
显示为 label
和 id
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过使用@JsonProperty?
Have you tried using @JsonProperty?
请注意,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 andcom.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.Jackson
如果您使用 Jackson,那么您可以使用
@JsonProperty
注释用于自定义给定 JSON 属性的名称。因此,您只需使用
@JsonProperty
注解对实体字段进行注解并提供自定义 JSON 属性名称,如下所示:JavaEE 或 JakartaEE JSON-B
JSON-B 是用于转换 Java 的标准绑定层对象与 JSON 之间的转换。如果您使用的是 JSON-B,则可以通过 < 覆盖 JSON 属性名称code>@JsonbProperty 注解:
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: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:还有一个选项可以重命名字段:
Jackson MixIns。
如果您处理第三方类,而您无法对其进行注释,或者您只是不想用 Jackson 特定的注释来污染该类,那么这很有用。
Mixins 的 Jackson 文档已经过时,因此这个 示例 可以提供更清晰的信息。本质上:您创建 mixin 类,它以您想要的方式进行序列化。然后将其注册到ObjectMapper:
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: