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("138@163.com");
        
        
        /** 
         * 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":"138@163.com"}
        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("123@qq.com");
        
        
        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\":\"xiaomin@sina.com\"} ";  
        
        /** 
         * ObjectMapper支持從byte[]、File、InputStream、字符串等數據的JSON反序列化。 
         */  
        ObjectMapper mapper = new ObjectMapper();
        User user = mapper.readValue(json, User.class);
        System.out.println(user); 
    }
}

 

  本文參考: https://blog.csdn.net/zmx729618/article/details/52161069


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM