如何将JSON字段名称映射到不同的对象字段名称? [英] How to map JSON field names to different object field names?
问题描述
以下jax-b注释的Jackson json注释中的等价方式是什么?
What is the equiv way in Jackson json annotation for the following jax-b annotations?
我需要生成json而不是xml并且需要知道传统的jackson在jax-b中等效表示的注释。
I need to produce json rather than xml and need to know the conventional jackson annotations that is equivalently denoted in jax-b.
- 重命名字段。
- 使用getters代替字段。
如果json / xml元素名称是java保留字
,则这些功能尤其重要 new
, public
, static
等。
These features are especially crucial if the json/xml element name is a java reserved word
like "new
", "public
", "static
", etc.
因此我们必须将POJO字段分别命名为_new_,_ public _,_ static_等,
So that we have to name the POJO fields as "_new_", "_public_", "_static_", etc, respectively,
但是使用jax-b注释将它们重命名为生成的XML(和json)元素中的new,public,static等等
。
but use jax-b annotation to rename them back to "new", "public", "static", etc in the generated XML (and json) elements.
重命名字段
@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_ ;
}
重定向到使用属性getter (我认为这是是如何在jax-b中完成的
Redirect to using property getter (I think this is how it is done in 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_;}
}
推荐答案
可能它有点迟了但是无论如何..
Probably it's a bit late but anyway..
你可以重命名一个属性只需添加
you can rename a property just adding
@JsonProperty("contractor")
默认情况下,杰克逊使用吸气剂和setter来序列化和反序列化。
And by default Jackson use the getter and setter to serialize and deserialize.
有关更多详细信息: http://wiki.fasterxml.com/JacksonFAQ
这篇关于如何将JSON字段名称映射到不同的对象字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!