Java 如何将 JSON 字段名称映射到不同的对象字段名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9741134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to map JSON field names to different object field names?
提问by Blessed Geek
What is the equiv way in Hymanson json annotation for the following jax-b annotations?
以下 jax-b 注释在 Hymanson json 注释中的等效方式是什么?
I need to produce json rather than xml and need to know the conventional Hymanson annotations that is equivalently denoted in jax-b.
我需要生成 json 而不是 xml,并且需要知道在 jax-b 中等效表示的常规 Hymanson 注释。
- rename a field.
- use getters instead of fields.
- 重命名字段。
- 使用 getter 而不是字段。
These features are especially crucial if the json/xml element name is a java reserved word
like "new
", "public
", "static
", etc.
如果 json/xml 元素名称是 Java 保留字,如“ new
”、“ public
”、“ static
”等,则这些功能尤其重要。
So that we have to name the POJO fields as "_new_", "_public_", "_static_", etc, respectively,
所以我们必须将 POJO 字段分别命名为“_new_”、“_public_”、“_static_”等,
but use jax-b annotation to rename them back to "new", "public", "static", etc in the generated XML (and json) elements.
但是在生成的 XML(和 json)元素中使用 jax-b 注释将它们重命名回“new”、“public”、“static”等。
Renaming a field
重命名字段
@XmlAccessorType(XmlAccessType.FIELD)
public class Person{
@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected String address;
@XmlElement(name = "contractor")
protected boolean _restricted_ ;
@XmlElement(name = "new")
protected boolean _new_ ;
}
Redirect to using property getter(I think this is how it is done in jax-b)
重定向到使用属性 getter(我认为这是在 jax-b 中完成的方式)
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Person{
protected String name;
protected String address;
protected boolean _restricted_ ;
protected boolean _new_ ;
@XmlElement(required = true)
protected String getName() {return name;}
@XmlElement(required = true)
protected String getAddress() {return address;}
@XmlElement(name = "contractor")
protected boolean getRestricted() {return _restricted_;}
@XmlElement(name = "new")
protected boolean getNew(){return _new_;}
}
采纳答案by Enrichman
Probably it's a bit late but anyway..
可能有点晚了,但无论如何..
you can rename a property just adding
你可以重命名一个属性只是添加
@JsonProperty("contractor")
And by default Hymanson use the getter and setter to serialize and deserialize.
默认情况下,Hymanson 使用 getter 和 setter 来序列化和反序列化。
For more detailed information: http://wiki.fasterxml.com/HymansonFAQ
更多详细信息:http: //wiki.fasterxml.com/HymansonFAQ
回答by Vijai
With some example, You can also use it in getter and setter to rename it to different field
举个例子,你也可以在 getter 和 setter 中使用它来将它重命名为不同的字段
public class Sample {
private String fruit;
@JsonProperty("get_apple")
public void setFruit(String fruit) {
this.fruit = fruit;
}
@JsonProperty("send_apple")
public String getFruit() {
return fruit;
}
}
回答by Florin As?voaie
Please note that the proper JavaEE API for this is to use the javax.json.bind.annotation.JsonbProperty
annotation. Of course Hymanson and others are just some implementations of the JSON Binding API, they will likely comply with this.
请注意,正确的 JavaEE API 是使用javax.json.bind.annotation.JsonbProperty
注释。当然,Hymanson 和其他人只是 JSON Binding API 的一些实现,他们很可能会遵守这一点。