GSON JavaオブジェクトのJSONシーケンス化と逆シーケンス化を実現するインスタンスチュートリアル

7345 ワード

GitHubからGSONをダウンロード:https://github.com/google/gsonGsonの応用は主にtoJsonとfromJsonの2つの変換関数であり、このようなオブジェクト変換を使用する前にオブジェクトのカテゴリを作成し、そのメンバーがJSON文字列を対応するオブジェクトに変換することに成功する必要がある.

 class Examples {
  private int answer1 = 100;
  private String answer2 = "Hello world!";
  Examples(){
  }   // default constructor
 }

シーケンス化JAVAオブジェクトJSON文字列

  Examples example1 = new Examples();
  Gson gson = new Gson();
  String json = gson.toJson(example1);

jsonの結果は

{"answer1":100,"answer2":"Hello world!"}

逆シーケンス化JSON文字列対応JAVAオブジェクト

Examples example2= gson.fromJson(json,Examples.class);

=>>example 2つまりcexample 1と同じ
オブジェクトexample 1はtoJsonによってJSON文字列にシーケンス化されて伝達され、1つのオブジェクトexample 2がJSONを受信した後fromJsonによって逆シーケンス化されてexample 2になることを宣言するので、example 1はexample 2と同じである
例:

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collection; 
import java.util.HashMap; 
import java.util.Map; 
  
import com.google.gson.Gson; 
import com.google.gson.reflect.TypeToken; 
  
class User{ 
  public User(String name,int age,StringBuffer sex,boolean isChild){ 
   this.name = name; 
   this.age = age; 
   this.sex = sex; 
   this.isChild = isChild; 
  } 
  private String name; 
  private int age; 
  private StringBuffer sex; 
  private boolean isChild; 
  public String toString(){ 
   return "{name="+name+";age="+age+";sex="+sex+";isChild="+isChild+"}"; 
  } 
  public int hashCode(){ 
   return name.hashCode()*100+age; 
  } 
} 
  
public class GsonTest { 
  public static void main(String[] args) { 
   Gson gson = new Gson(); 
    
   System.out.println("1   Bean   **************************"); 
   System.out.println("   Bean    json   ->"); 
   User user1 = new User("  ",12,new StringBuffer("  "),true); 
   System.out.println("     user1"+user1); 
   String json = gson.toJson(user1); 
   System.out.println("User      Json   ,json==="+json); 
    
   System.out.println("***************************"); 
   System.out.println("   json       Bean->"); 
   User user2 = gson.fromJson(json, User.class); 
   System.out.println("     user2=="+user2); 
   System.out.println(); 
    
   System.out.println("2Collection     **************************"); 
   System.out.println("   Bean List      json   ->"); 
   Collection userList1 = new ArrayList(); 
   for(int i=0;i<3;i++){ 
    User user = new User("  ",10+i,new StringBuffer(" "),false); 
    userList1.add(user); 
   } 
   json = gson.toJson(userList1); 
   System.out.println("User List        Json   ,json==="+json); 
    
   System.out.println("***************************"); 
   System.out.println("   json       Bean List  ->"); 
   Collection userList2 = 
      gson.fromJson(json, 
         new TypeToken>(){}.getType()); 
   System.out.println("     User List  ,userList2="+userList2); 
   System.out.println(); 
    
   System.out.println("3Array     **************************"); 
   System.out.println("   Bean Array      json   ->"); 
   User [] userArray1 = new User[3]; 
   for(int i=0;i"); 
   User [] userArray2 = gson.fromJson(json, new TypeToken(){}.getType()); 
   System.out.println("     User     ,userArray2="+Arrays.toString(userArray2)); 
   System.out.println(); 
    
   System.out.println("4Map   **************************"); 
   System.out.println("   Bean Map    json   ->"); 
   Map map1 = new HashMap(); 
   for(int i=0;i<3;i++){ 
    map1.put(""+(i+10), userArray1[i]); 
   } 
   json = gson.toJson(map1); 
   System.out.println("User Map      Json   ,json==="+json); 
    
   System.out.println("***************************"); 
   System.out.println("   json       Bean     ->"); 
   Map map2 = 
      gson.fromJson(json, 
         new TypeToken>(){}.getType()); 
   System.out.println("     User     ,map2=="+map2); 
  } 
} 

実行結果:

1   Bean   ************************** 
   Bean    json   -> 
     user1{name=  ;age=12;sex=  ;isChild=true} 
User      Json   ,json==={"name":"  ","age":12,"sex":"  ","isChild":true} 
*************************** 
   json       Bean-> 
     user2=={name=  ;age=12;sex=  ;isChild=true} 
 
2Collection     ************************** 
   Bean List      json   -> 
User List        Json   ,json===[{"name":"  ","age":10,"sex":" ","isChild":false},{"name":"  ","age":11,"sex":" ","isChild":false},{"name":"  ","age":12,"sex":" ","isChild":false}] 
*************************** 
   json       Bean List  -> 
     User List  ,userList2=[{name=  ;age=10;sex= ;isChild=false}, {name=  ;age=11;sex= ;isChild=false}, {name=  ;age=12;sex= ;isChild=false}] 
 
3Array     ************************** 
   Bean Array      json   -> 
User         Json   ,json===[{"name":"  ","age":20,"sex":"  ","isChild":true},{"name":"  ","age":20,"sex":"  ","isChild":true},{"name":"  ","age":20,"sex":"  ","isChild":true}] 
*************************** 
   json       Bean     -> 
     User     ,userArray2=[{name=  ;age=20;sex=  ;isChild=true}, {name=  ;age=20;sex=  ;isChild=true}, {name=  ;age=20;sex=  ;isChild=true}] 
 
4Map   ************************** 
   Bean Map    json   -> 
User Map      Json   ,json==={"10":{"name":"  ","age":20,"sex":"  ","isChild":true},"11":{"name":"  ","age":20,"sex":"  ","isChild":true},"12":{"name":"  ","age":20,"sex":"  ","isChild":true}} 
*************************** 
   json       Bean     -> 
     User     ,map2=={10={name=  ;age=20;sex=  ;isChild=true}, 11={name=  ;age=20;sex=  ;isChild=true}, 12={name=  ;age=20;sex=  ;isChild=true}}