Jackson序列化和反序列化

  1,下载Jackson工具包(jackson-core-2.2.3.jar  jackson-annotations-2.2.3.jar  jackson-databind-2.2.3.jar )

jackson-core-2.2.3.jar 核心包
http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.2.3/jackson-core-2.2.3.jar

jackson-annotations-2.2.3.jar 提供Json注解支持
http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.2.3/jackson-annotations-2.2.3.jar

jackson-databind-2.2.3.jar
http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.2.3/jackson-databind-2.2.3.jar

  2,JSON序列化和反序列化使用的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 {

    /**
     * JSON注解 Jackson提供了一系列注解,方便对JSON序列化和反序列化进行控制,下面介绍一些常用的注解。
     * 
     * @JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性。
     * @JsonFormat 此注解用于属性上,作用是把Date类型直接转化为想要的格式,如@JsonFormat(pattern = "yyyy-MM-dd
     *             HH-mm-ss")。
     * @JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化为name,@JsonProperty("name")。
     */

    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
    }

}

  3,Jackson序列化和反序列化的使用

package com.st.json;

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

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


/** 
 * @Description: Jackson的使用   JSON[序列化]和[反序列化]
 * @author Mr.Li
 * @date 2018年4月21日 下午11:05:31 
 */
public class JacksonDemo {
    
    public static void main(String[] args) throws ParseException, IOException {
        jsonTest();
    }
    
    /**
     * jackson序列化的使用
     * @throws ParseException
     * @throws JsonProcessingException
     */
    public static void jackTest() throws ParseException, JsonProcessingException {
        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]");
        
        
        /** 
         * ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。 
         * ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。 
         * writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。 
         * writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。 
         * writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。 
         * writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。 
         */
        ObjectMapper mapper = new ObjectMapper();
        
        //User对象转Json,
        //输出{"id":1,"name":"curry","age":30,"birthday":590774400000,"email":"[email protected]"}
        String jsonValue = mapper.writeValueAsString(u);
        System.out.println(jsonValue);
        
        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);
        String jsonList = mapper.writeValueAsString(users);
        System.out.println(jsonList);
        
    }
    
    /**
     * JSON转Java对象[JSON反序列化]
     * @throws IOException 
     * @throws JsonMappingException 
     * @throws JsonParseException 
     */

    public static void jsonTest() throws JsonParseException, JsonMappingException, IOException {
        String json = " {\"id\":3, \"name\":\"小明\", \"age\":18, \"birthday\":590774400000, \"email\":\"[email protected]\"} ";  
        
        /** 
         * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。 
         */  
        ObjectMapper mapper = new ObjectMapper();
        User user = mapper.readValue(json, User.class);
        System.out.println(user); 
    }
}

猜你喜欢

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