首頁  >  文章  >  Java  >  解析Java的Jackson函式庫中物件的序列化與資料泛型綁定

解析Java的Jackson函式庫中物件的序列化與資料泛型綁定

高洛峰
高洛峰原創
2017-01-18 10:48:361382瀏覽

Jackson物件序列化
這裡將介紹將Java物件序列化到一個JSON文件,然後再讀取JSON文件取得轉換為物件。在這個例子中,創建了Student類別。建立將有學生物件以JSON表示在一個student.json檔案。

建立一個名為JacksonTester在Java類別檔案在C:>Jackson_WORKSPACE.

檔案: JacksonTester.java

import java.io.File;
import java.io.IOException;
 
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
 
public class JacksonTester {
  public static void main(String args[]){
   JacksonTester tester = new JacksonTester();
   try {
     Student student = new Student();
     student.setAge(10);
     student.setName("Mahesh");
     tester.writeJSON(student);
 
     Student student1 = tester.readJSON();
     System.out.println(student1);
 
   } catch (JsonParseException e) {
     e.printStackTrace();
   } catch (JsonMappingException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
  }
 
  private void writeJSON(Student student) throws JsonGenerationException, JsonMappingException, IOException{
   ObjectMapper mapper = new ObjectMapper(); 
   mapper.writeValue(new File("student.json"), student);
  }
 
  private Student readJSON() throws JsonParseException, JsonMappingException, IOException{
   ObjectMapper mapper = new ObjectMapper();
   Student student = mapper.readValue(new File("student.json"), Student.class);
   return student;
  }
}
 
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+ " ]";
  } 
}

驗證結果

使用javac 編譯如下類:

C:\Jackson_WORKSPACE>javac JacksonTester.java

驗證輸出結果

C:\Jackson_WORKSPACE>java JacksonTester

Jackson資料綁定泛型

在簡單的資料綁定中,我們使用String作為關鍵對象,並作為一個值對象映射類別。相反,我們可以使用具體的Java物件和類型強制轉換到JSON使用。



考慮下面的範例使用一個類別的UserData來保存使用者專用資料。

建立一個名為JacksonTester在Java類別檔案目錄C:>Jackson_WORKSPACE.

檔案名稱: JacksonTester.java

Student [ name: Mahesh, age: 10 ]

驗證輸出

使用javacester.Tester

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
 
public class JacksonTester {
  public static void main(String args[]){
   JacksonTester tester = new JacksonTester();
     try {
      ObjectMapper mapper = new ObjectMapper();
 
      Map userDataMap = new HashMap();
      UserData studentData = new UserData(); 
      int[] marks = {1,2,3};
 
      Student student = new Student();
      student.setAge(10);
      student.setName("Mahesh");
      // JAVA Object
      studentData.setStudent(student);
      // JAVA String
      studentData.setName("Mahesh Kumar");
      // JAVA Boolean
      studentData.setVerified(Boolean.FALSE);
      // Array
      studentData.setMarks(marks);
      TypeReference ref = new TypeReference>() { };
      userDataMap.put("studentData1", studentData);
      mapper.writeValue(new File("student.json"), userDataMap);
      //{
      //  "studentData1":
      // {
      // "student":
      // {
      //  "name":"Mahesh",
      //  "age":10
      //   },
      //   "name":"Mahesh Kumar",
      //   "verified":false,
      //   "marks":[1,2,3]
      //  }
      //}
      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;
  } 
}

驗證輸出

C:\Jackson_WORKSPACE>javac JacksonTester.java

更多解析Java的Jackson庫中物件的序列化與資料泛型綁定相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn