Jackson json 处理全大写或不规范的JSON

面对不遵照驼峰命名规则的接口咋办?固然首先要吐槽一下,不过接口是别人定的,虽然看着不爽但仍是得去适配,好比cardNumber,他返回的叫{CARDNUMBER:''}。html

经过对API的研究能够经过@JsonProperty以及@JsonAutoDetect来实现。java

先看代码api

@JsonAutoDetect(JsonMethod.FIELD)
public class MemberApiParameter implements Serializable {

	private static final long serialVersionUID = 1L;

	/** 姓名 **/
	@JsonProperty("NAME")
	private String name;

	/** 性别 **/
	@JsonProperty("SEX")
	private String sex;

        public String getName() {
		return name;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
}

 

 @JsonProperty("NAME")顾名思义,就是显示指定字段的别名,无论是输入仍是输出都是这个名字。oracle

 @JsonAutoDetect(JsonMethod.FIELD)这个的意思是指解析字段,若是不这样设置,有兴趣的朋友能够试一下,会输出两套东西,相似{name:'',NAME:''},也就是说字段和getter方法都解析了,因此须要制定只解析字段名,忽略方法。还有一种方法就是须要一行行的在全部getter上加上@JsonIgnore,若是字段多就累死了。this

JsonMethod的API说明:spa

Enum Constant Summarycode

ALL 
          This pseudo-type indicates that all of real types are included
CREATOR 
          Creators are constructors and (static) factory methods used to construct POJO instances for deserialization
FIELD 
          Field refers to fields of regular Java objects.
GETTER 
          Getters are methods used to get a POJO field value for serialization, or, under certain conditions also for de-serialization.
IS_GETTER 
          "Is getters" are getter-like methods that are named "isXxx" (instead of "getXxx" for getters) and return boolean value (either primitive, or Boolean).
NONE 
          This pseudo-type indicates that none of real types is included
SETTER            Setters are methods used to set a POJO value for deserialization.
相关文章
相关标签/搜索