博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Json解析工具Jackson(使用注解)
阅读量:4037 次
发布时间:2019-05-24

本文共 3589 字,大约阅读时间需要 11 分钟。

接上一篇文章Json解析工具Jackson(简单应用),jackson在实际应用中给我们提供了一系列注解,提高了开发的灵活性,下面介绍一下最常用的一些注解
@JsonIgnoreProperties
         此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。
@JsonIgnore
         此注解用于属性或者方法上(最好是属性上),作用和上面的@JsonIgnoreProperties一样。
@JsonFormat
        此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式,比如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")
@JsonSerialize
        此注解用于属性或者getter方法上,用于在序列化时嵌入我们自定义的代码,比如序列化一个double时在其后面限制两位小数点。
[java] view plaincopy
public class CustomDoubleSerialize extends JsonSerializer<Double> {  
  
    private DecimalFormat df = new DecimalFormat("##.00");  
  
    @Override  
    public void serialize(Double value, JsonGenerator jgen,  
            SerializerProvider provider) throws IOException,  
            JsonProcessingException {  
  
        jgen.writeString(df.format(value));  
    }  
}  
@JsonDeserialize
         此注解用于属性或者setter方法上,用于在反序列化时可以嵌入我们自定义的代码,类似于上面的@JsonSerialize
[java] view plaincopy
public class CustomDateDeserialize extends JsonDeserializer<Date> {  
  
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  
    @Override  
    public Date deserialize(JsonParser jp, DeserializationContext ctxt)  
            throws IOException, JsonProcessingException {  
  
        Date date = null;  
        try {  
            date = sdf.parse(jp.getText());  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
        return date;  
    }  
}  
完整例子
       
[java] view plaincopy
//表示序列化时忽略的属性  
@JsonIgnoreProperties(value = { "word" })  
public class Person {  
    private String name;  
    private int age;  
    private boolean sex;  
    private Date birthday;  
    private String word;  
    private double salary;  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int age) {  
        this.age = age;  
    }  
  
    public boolean isSex() {  
        return sex;  
    }  
  
    public void setSex(boolean sex) {  
        this.sex = sex;  
    }  
  
    public Date getBirthday() {  
        return birthday;  
    }  
  
    // 反序列化一个固定格式的Date  
    @JsonDeserialize(using = CustomDateDeserialize.class)  
    public void setBirthday(Date birthday) {  
        this.birthday = birthday;  
    }  
  
    public String getWord() {  
        return word;  
    }  
  
    public void setWord(String word) {  
        this.word = word;  
    }  
  
    // 序列化指定格式的double格式  
    @JsonSerialize(using = CustomDoubleSerialize.class)  
    public double getSalary() {  
        return salary;  
    }  
  
    public void setSalary(double salary) {  
        this.salary = salary;  
    }  
  
    public Person(String name, int age) {  
        this.name = name;  
        this.age = age;  
    }  
  
    public Person(String name, int age, boolean sex, Date birthday,  
            String word, double salary) {  
        super();  
        this.name = name;  
        this.age = age;  
        this.sex = sex;  
        this.birthday = birthday;  
        this.word = word;  
        this.salary = salary;  
    }  
  
    public Person() {  
    }  
  
    @Override  
    public String toString() {  
        return "Person [name=" + name + ", age=" + age + ", sex=" + sex  
                + ", birthday=" + birthday + ", word=" + word + ", salary="  
                + salary + "]";  
    }  
  
}  
[java] view plaincopy
public class Demo {  
    public static void main(String[] args) {  
  
        writeJsonObject();  
  
        // readJsonObject();  
    }  
  
    // 直接写入一个对象(所谓序列化)  
    public static void writeJsonObject() {  
        ObjectMapper mapper = new ObjectMapper();  
        Person person = new Person("nomouse", 25, true, new Date(), "程序员",  
                2500.0);  
        try {  
            mapper.writeValue(new File("c:/person.json"), person);  
        } catch (JsonGenerationException e) {  
            e.printStackTrace();  
        } catch (JsonMappingException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    // 直接将一个json转化为对象(所谓反序列化)  
    public static void readJsonObject() {  
        ObjectMapper mapper = new ObjectMapper();  
  
        try {  
            Person person = mapper.readValue(new File("c:/person.json"),  
                    Person.class);  
            System.out.println(person.toString());  
        } catch (JsonParseException e) {  
            e.printStackTrace();  
        } catch (JsonMappingException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
  

转载地址:http://efjdi.baihongyu.com/

你可能感兴趣的文章
查找算法
查看>>
C语言单链表实现
查看>>
SQL基本命令集合整理
查看>>
QT中json的生成和解析
查看>>
std::function 和 std::bind 的简单例子
查看>>
CFormView简介
查看>>
Visual Studio 2010 与 VC++ 6.0 的操作差异(一)之对话框中添加OnInitDialog()函数
查看>>
VC的MFC里面控件的ID使用ID_XXXXX和IDR_XXXXX的区别
查看>>
VC++ 获取ListControl选中行
查看>>
用VC++实现应用程序窗口的任意分割(2)
查看>>
“class”类型重定义,include(头文件)重复加载 QT /c++
查看>>
MFC框架类、文档类、视图类相互访问的方法
查看>>
<转>文档视图指针互获
查看>>
C++中头文件相互包含的几点问题
查看>>
内存设备描述表
查看>>
Latex插入eps图片的方法
查看>>
Matlab subplot 图像间距调整
查看>>
Hibernate使用count(*)取得表中记录总数
查看>>
distinct使SQL查询除去重复的字段
查看>>
从mysql中 导出/导入表及数据
查看>>