Jackson 解析json的一些用法
来源:互联网 发布:自学编程视频网站 编辑:程序博客网 时间:2024/06/04 19:56
1、问题:如何让Jackson 对枚举中中特定字段进行解析?
经常我们用枚举来表示某组相关属性值。
举个栗子:
/** * 性别类型 */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。阅读全文
0 0
- Jackson 解析json的一些用法
- JSON的解析[Jackson]
- Jackson的使用(json解析)
- jackson解析的一些参数
- json解析的一些常见用法
- Jackson的json转换和Jackson Annotations 基本用法
- jackson 用流的形式解析json
- Jackson解析首字母大写的json字符串
- Jackson解析首字母大写的json字符串
- Jackson解析首字母大写的json字符串
- Json解析之jackson的使用
- jackson解析 json字符串
- Jackson解析JSON例子
- Jackson解析JSON例子
- java json解析-jackson
- jackson框架解析json
- Jackson解析JSON数据
- Json解析工具Jackson
- hdu 1269 迷宫城堡(强连通分量)
- 在MATLAB中的图例标注及实例说明
- 线性表的顺序存储表示时的插入与删除
- Java:匿名内部类和Lambda表达式
- LeetCode-189:Rotate Array (数组的循环移位)
- Jackson 解析json的一些用法
- 第10章 32 位 Intel 微处理器编程架构
- new和malloc的区别
- centos 7 上配置mysql 开机启动详解
- 二叉树简单实现
- 连续总结第八天
- 多态的产生、静态,动态联编
- Secondary NameNode:它究竟有什么作用?
- (未完成)了解LSTM网络(Understanding LSTM Networks)