Jackson中objectMapper的使用详解

环境

<dependency>
      <groupId>org.codehaus.jacksongroupId>
      <artifactId>jackson-mapper-aslartifactId>
      <version>1.9.13version>
    dependency>

ObjectMapper作用

jackson主要是用来把对象转换成为一个json字符串返回到前端,或者把json字符串转化为对象.
现在大部分数据交换都是以json来传输的.加上现在es的使用越来越多,该类还是很重要的.

代码学习

  • Person对象
package cn.zhangyu.entity;

import java.util.Date;

public class Person {

    private int id;
    private String name;
    private Date date;

    public Person() {
    }

    public Person(int id, String name, Date date) {
        this.id = id;
        this.name = name;
        this.date = date;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", date=" + date +
                '}';
    }
}

常用方法:

  1. readValue()
  2. writeValueAsString
package cn.zhangyu;

import cn.zhangyu.entity.Person;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ObjectMapper
 * 主要是用来把对象转换成为一个json字符串返回到前端
 *
 * 将string串转化为对象
 * mapper.readValue(jsonString, Person.class);
 *
 * 将对象转化为json格式的字符串。
 * mapper.writeValueAsString(person);
 *
 *
 * Include.ALWAYS  是序列化对像所有属性(默认)
 *
 * Include.NON_NULL 只有不为null的字段才被序列化,属性为NULL 不序列化
 *
 * Include.NON_EMPTY 如果为null或者 空字符串和空集合都不会被序列化
 *
 * Include.NON_DEFAULT 属性为默认值不序列化
 */
public class App {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        //序列化的时候序列对象的所有属性
        mapper.setSerializationInclusion(JsonSerialize.Inclusion.ALWAYS);

        Person person = new Person(1,"张山",new Date());

        //把一个对象转化为json字符串
        String personJson = mapper.writeValueAsString(person);

        System.out.println(personJson);

        System.out.println("------------------------------------------");

        //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
        mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

        Person person1 = new Person(1,"张山",new Date());
        //把一个对象转化为json字符串
        String personJson1 = mapper.writeValueAsString(person1);
        System.out.println(personJson1);

        System.out.println("------------------------------------------");

        //把json字符串转化为Person对象
        String jsonString = "{\"id\":2,\"name\":\"李四\", \"date\":1560166271801}";
        Person person2 = mapper.readValue(jsonString, Person.class);
        System.out.println(person2);


    }
}

  • 结果:
{"id":1,"name":"张山","date":1560167332711}
------------------------------------------
{"id":1,"name":"张山","date":"2019-06-10 19:48:52"}
------------------------------------------
Person{id=2, name='李四', date=Mon Jun 10 19:31:11 CST 2019}

你可能感兴趣的:(java,ee,java,se,jsckson,objectMapper)