当前位置: 首页 > 教程 > Jackson >

Jackson-泛型数据绑定

精华
小牛编辑
139浏览
2023-03-14

在简单的数据绑定中,我们使用了Map类,该类使用String作为键,使用Object作为值对象。取而代之的是,我们可以换成一个具体类型的Java对象,然后将其类型转换为JSON。

1 泛型数据绑定的示例

1.1 编写核心类

MainApp:

package cn.xnip.jackson;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 小牛知识库网 - https://www.xnip.cn
 */
public class MainApp {

    public static void main(String args[]){
        MainApp tester = new MainApp();
        try {
            ObjectMapper mapper = new ObjectMapper();

            Map<String, UserData> userDataMap = new HashMap<String, UserData>();
            UserData studentData = new UserData();
            int[] marks = {1,2,3};

            Student student = new Student();
            student.setAge(10);
            student.setName("eric");

            // Java对象
            studentData.setStudent(student);
            // String类型
            studentData.setName("eric");
            // Boolean类型
            studentData.setVerified(Boolean.FALSE);
            // 数组类型
            studentData.setMarks(marks);
            TypeReference ref = new TypeReference<Map<String,UserData>>() { };
            userDataMap.put("studentData1", studentData);
            mapper.writeValue(new File("student.json"), userDataMap);

            userDataMap = mapper.readValue(new File("student.json"), ref);

            System.out.println(userDataMap.get("studentData1").getStudent());
            System.out.println(userDataMap.get("studentData1").getName());
            System.out.println(userDataMap.get("studentData1").getVerified());
            System.out.println(Arrays.toString(userDataMap.get("studentData1").getMarks()));
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

class Student {
    private String name;
    private int age;
    public Student(){}
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String toString(){
        return "Student [ name: "+name+", age: "+ age+ " ]";
    }
}

class UserData {
    private Student student;
    private String name;
    private Boolean verified;
    private int[] marks;

    public UserData(){}

    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Boolean getVerified() {
        return verified;
    }
    public void setVerified(Boolean verified) {
        this.verified = verified;
    }
    public int[] getMarks() {
        return marks;
    }
    public void setMarks(int[] marks) {
        this.marks = marks;
    }
}

1.2 运行测试