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);
    }

}

猜你喜欢

转载自www.cnblogs.com/QW-lzm/p/8904991.html