Jackson 解析json的一些用法
2017-10-19 21:34
141 查看
1、问题:如何让Jackson 对枚举中中特定字段进行解析?
经常我们用枚举来表示某组相关属性值。
举个栗子:
这里,我们希望Jackson解析GenderType时,只将对应类型的value解析出来,通过在属性的getValue()方法上加@JsonValue,这样设置的枚举值在通过Jackson解析时,会自动将枚举替换成对应的value值。
举个栗子:
结果输出:
{"name":"tim","genderType":2}
分析:上面我们将Student的类的性别字段设置为枚举类型,在通过Jackson解析成json数据时,因为设置了@JsonValue因此会解析成我们需要的值。
2、问题:如何让Jackson 当转换对象为json时,如果属性为null时,不输出?
举个栗子:
Student.java 类:
输出:
{"name":"tim"}
分析:
上述通过加入@JsonSerialize(include =JsonSerialize.Inclusion.NON_NULL)注解,这样当我们的示例student虽然只设置name字段值为”tim”,此时,sex字段的值为null,但是Jackson通过上述注解忽略了字段值为null。
3、问题:更改字段的显示值?
通过加入注解:@JsonProperty(“”)
举个栗子:
输出:
{"name":"tim","error_code":0}
分析:
通过加入@JsonProperty("error_code")注解,能将之前字段名为errorCode,显示的时候改为error_code。
经常我们用枚举来表示某组相关属性值。
举个栗子:
/** * 性别类型 */ public enum GenderType { GENDER_UNKNOWN(0, "未知"), GENDER_MALE(1, "男性"), GENDER_FEMAL(2, "女性"), GENDER_OTHER(3, "其他"), ; private int value; private String comment; private GenderType(int value, String comment){ this.value = value; this.comment = comment; } @JsonValue public int getValue() { return value; } public String getComment() { return comment; } }
这里,我们希望Jackson解析GenderType时,只将对应类型的value解析出来,通过在属性的getValue()方法上加@JsonValue,这样设置的枚举值在通过Jackson解析时,会自动将枚举替换成对应的value值。
举个栗子:
public class Student { private String name; private GenderType genderType; public String getName() { return name; } public void setName(String name) { this.name = name; } public GenderType getGenderType() { return genderType; } public void setGenderType(GenderType genderType) { this.genderType = genderType; } public static void main(String[] args) throws Exception{ Student student = new Student(); student.setName("tim"); student.setGenderType(GenderType.GENDER_FEMAL); System.out.println(JsonUtils.writeObject2Json(student)); } }
结果输出:
{"name":"tim","genderType":2}
分析:上面我们将Student的类的性别字段设置为枚举类型,在通过Jackson解析成json数据时,因为设置了@JsonValue因此会解析成我们需要的值。
2、问题:如何让Jackson 当转换对象为json时,如果属性为null时,不输出?
通过在实体前加入:@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
举个栗子:
Student.java 类:
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) public class Student { private String name; 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; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } public static void main(String[] args) throws JsonProcessingException { Student student = new Student(); student.setName("tim"); System.out.println(JsonUtils.writeObject2Json(student)); } }
输出:
{"name":"tim"}
分析:
上述通过加入@JsonSerialize(include =JsonSerialize.Inclusion.NON_NULL)注解,这样当我们的示例student虽然只设置name字段值为”tim”,此时,sex字段的值为null,但是Jackson通过上述注解忽略了字段值为null。
3、问题:更改字段的显示值?
通过加入注解:@JsonProperty(“”)
举个栗子:
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) public class Student { @JsonProperty("error_code") private int errorCode; private String name; private String sex; public int getErrorCode() { return errorCode; } public void setErrorCode(int errorCode) { this.errorCode = errorCode; } 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; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } public static void main(String[] args) throws JsonProcessingException { Student student = new Student(); student.setErrorCode(0); student.setName("tim"); System.out.println(JsonUtils.writeObject2Json(student)); } }
输出:
{"name":"tim","error_code":0}
分析:
通过加入@JsonProperty("error_code")注解,能将之前字段名为errorCode,显示的时候改为error_code。
相关文章推荐
- json解析的一些常见用法
- AFN的一些常见的用法(GET,POST)解析(XML,Json)
- Json解析工具Jackson(使用注解)--jackson框架自定义的一些json解析注解
- Java下利用Jackson进行JSON解析和序列化,注解应用
- jQuery each用法及each解析json
- 用Js的eval解析JSON用法
- json解析TypeToken的用法(最外层是集合)
- 比Gson解析速度快10倍!——Json解析神器Jackson使用介绍
- Json Jackson 在存储时忽视掉一些引用属性
- Android数据格式解析对象JSON用法
- Android数据格式解析对象JSON用法
- Java下利用Jackson进行JSON解析和序列化
- SSM框架一些配置用法解析(简单说明)
- Json解析工具Jackson(简单应用)
- 把实体 转为json 数据格式---jackson 的详细用法.
- Java下利用Jackson进行JSON解析和序列化
- Json解析之jackson的使用
- JackSon解析json中文出现的编码问题
- Android数据格式解析对象JSON用法(转)
- Json解析工具Jackson(使用注解)