Jackson 对象与json数据互转工具类JacksonUtil

Posted qw-lzm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jackson 对象与json数据互转工具类JacksonUtil相关的知识,希望对你有一定的参考价值。

  1,User对象

package com.st.json;

import java.util.Date;

/**
 * @Description: JSON序列化和反序列化使用的User类
 * @author Mr.Li
 * @date 2018年4月21日 下午10:55:34
 */
public class User {

    private Integer id;
    private String name;
    private Integer age;
    private Date birthday;
    private String email;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + ", birthday=" + birthday + ", email=" + email
                + "]";
    }

    public User(Integer id, String name, Integer age, Date birthday, String email) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
        this.email = email;
    }

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

}

  2,JacksonUtil 工具类

package com.st.json;

import java.io.IOException;
import java.io.StringWriter;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/** 
 * @Description: Jackson 对象与json互转工具类
 * @author Mr.Li
 * @date 2018年4月22日 上午12:30:15 
 */
public class JacksonUtil {
    
    private static ObjectMapper mapper = new ObjectMapper();  
    
    public static String BeanToJson(Object obj) throws IOException {
        StringWriter sw = new StringWriter();
        JsonGenerator gen = new JsonFactory().createGenerator(sw);
        mapper.writeValue(gen, obj);
        gen.close();
        return sw.toString();
    }  
  
    public static <T> T JsonToBean(String jsonStr, Class<T> objClass)  
            throws JsonParseException, JsonMappingException, IOException {  
        return mapper.readValue(jsonStr, objClass);  
    }

}

  3,Jackson 对象与json互转测试

package com.st.json;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;


/** 
 * @Description: Jackson 对象与json互转测试
 * @author Mr.Li
 * @date 2018年4月22日 上午12:39:39 
 */
public class JacksonUtilTest {
    
    
    public static void main(String[] args) throws ParseException, IOException {
        beanTojack();
//        jsonTobean();
    }
    
    public static void beanTojack() throws ParseException, IOException {
        
        User u = new User();
        u.setId(1);
        u.setName("curry");
        u.setAge(30);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        u.setBirthday(dateFormat.parse("1988-9-21"));
        u.setEmail("[email protected]");
        
        User u2 = new User();
        u2.setId(2);
        u2.setName("KD");
        u2.setAge(29);
        u2.setBirthday(dateFormat.parse("1989-9-21"));
        u2.setEmail("[email protected]");
        
        List<User> users = new ArrayList<>();
        users.add(u);
        users.add(u2);
        
        //对象转json格式
        String beanToJson = JacksonUtil.BeanToJson(users);
        System.out.println(beanToJson);
    }
    
    public static void jsonTobean() throws JsonParseException, JsonMappingException, IOException {
        String json = " {\"id\":3, \"name\":\"小明\", \"age\":18, \"birthday\":590774400000, \"email\":\"[email protected]\"} ";
        
        User jsonToBean = JacksonUtil.JsonToBean(json, User.class);
        System.out.println(jsonToBean);
    }

}

 

以上是关于Jackson 对象与json数据互转工具类JacksonUtil的主要内容,如果未能解决你的问题,请参考以下文章

总结的一些json格式和对象/String/Map/List等的互转工具类

java对象与Json互转

利用JackSon工具将JSON文件和对象互转

jackson 常用注解,比如忽略某些属性,驼峰和下划线互转

利用Jackson封装常用JsonUtil工具类

Jackson处理对象和json之间转换的工具类