Google Gson的使用方法,實現Json結構的相互轉換

原文鏈接

Java開發中,有時需要保存一個數據結構成字符串,可能你會考慮用Json,但是當Json字符串轉換成Java對象時,轉換成的是JsonObject,並不是你想要的Class類型的對象,操作起來就很不是愉悅,下面說的就可以解決了這種問題。


首先,需要把Google的Gson的Jar包導入到項目中,這個導入包的簡單步驟就不展示了,Gson的下載鏈接:http://download.csdn.NET/detail/qxs965266509/8367275

現在,我先自定義一個Class類

[java] view plain copy
  1. public class Student {  
  2.     public int id;  
  3.     public String nickName;  
  4.     public int age;  
  5.     public ArrayList<String> books;  
  6.     public HashMap<String, String> booksMap;  
  7. }  

案例一,案例二,案例三都是把Java的Class對象使用Gson轉換成Json的字符串

案例一:

僅包含基本數據類型的數據結構

[java] view plain copy
  1. Gson gson = new Gson();  
  2.     Student student = new Student();  
  3.     student.id = 1;  
  4.     student.nickName = "喬曉鬆";  
  5.     student.age = 22;  
  6.     student.email = "[email protected]";  
  7.     Log.e("MainActivity", gson.toJson(student));  

輸出結果是 :

[java] view plain copy
  1. {"email":"[email protected]","nickName":"喬曉鬆","id":1,"age":22}  


案例二:

除了基本數據類型還包含了List集合

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. Gson gson = new Gson();  
  2.         Student student = new Student();  
  3.         student.id = 1;  
  4.         student.nickName = "喬曉鬆";  
  5.         student.age = 22;  
  6.         student.email = "[email protected]";  
  7.         ArrayList<String> books = new ArrayList<String>();  
  8.         books.add("數學");  
  9.         books.add("語文");  
  10.         books.add("英語");  
  11.         books.add("物理");  
  12.         books.add("化學");  
  13.         books.add("生物");  
  14.         student.books = books;  
  15.         Log.e("MainActivity", gson.toJson(student));  
輸出結果是 :
[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. {"books":["數學","語文","英語","物理","化學","生物"],"email":"[email protected]","nickName":"喬曉鬆","id":1,"age":22}  

案例三:

除了基本數據類型還包含了List和Map集合

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. Gson gson = new Gson();  
  2.         Student student = new Student();  
  3.         student.id = 1;  
  4.         student.nickName = "喬曉鬆";  
  5.         student.age = 22;  
  6.         student.email = "[email protected]";  
  7.         ArrayList<String> books = new ArrayList<String>();  
  8.         books.add("數學");  
  9.         books.add("語文");  
  10.         books.add("英語");  
  11.         books.add("物理");  
  12.         books.add("化學");  
  13.         books.add("生物");  
  14.         student.books = books;  
  15.         HashMap<String, String> booksMap = new HashMap<String, String>();  
  16.         booksMap.put("1""數學");  
  17.         booksMap.put("2""語文");  
  18.         booksMap.put("3""英語");  
  19.         booksMap.put("4""物理");  
  20.         booksMap.put("5""化學");  
  21.         booksMap.put("6""生物");  
  22.         student.booksMap = booksMap;  
  23.         Log.e("MainActivity", gson.toJson(student));  

輸出結果是 :

[java] view plain copy
  1. {"books":["數學","語文","英語","物理","化學","生物"],"booksMap":{"3":"英語","2":"語文","1":"數學","6":"生物","5":"化學","4":"物理"},"email":"[email protected]","nickName":"喬曉鬆","id":1,"age":22}  


案例四:
把案例三輸出的字符串使用Gson轉換成Student對象

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. Gson gson = new Gson();  
  2.         Student student = new Student();  
  3.         student.id = 1;  
  4.         student.nickName = "喬曉鬆";  
  5.         student.age = 22;  
  6.         student.email = "[email protected]";  
  7.         ArrayList<String> books = new ArrayList<String>();  
  8.         books.add("數學");  
  9.         books.add("語文");  
  10.         books.add("英語");  
  11.         books.add("物理");  
  12.         books.add("化學");  
  13.         books.add("生物");  
  14.         student.books = books;  
  15.         HashMap<String, String> booksMap = new HashMap<String, String>();  
  16.         booksMap.put("1""數學");  
  17.         booksMap.put("2""語文");  
  18.         booksMap.put("3""英語");  
  19.         booksMap.put("4""物理");  
  20.         booksMap.put("5""化學");  
  21.         booksMap.put("6""生物");  
  22.         student.booksMap = booksMap;  
  23.         String result = gson.toJson(student);  
  24.   
  25.         Student studentG = gson.fromJson(result, Student.class);  
  26.   
  27.         Log.e("MainActivity""id:" + studentG.id);  
  28.         Log.e("MainActivity""nickName:" + studentG.nickName);  
  29.         Log.e("MainActivity""age:" + studentG.age);  
  30.         Log.e("MainActivity""email:" + studentG.email);  
  31.         Log.e("MainActivity""books size:" + studentG.books.size());  
  32.         Log.e("MainActivity""booksMap size:" + studentG.booksMap.size());  
輸出結果是 :

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. id:1  
  2. nickName:喬曉鬆  
  3. age:22  
  4. email:[email protected]  
  5. books size:6  
  6. booksMap size:6  

通過這4個案例我解決你一定就把Gson的基本用法學會了,當然我們的需求可能需要把List或者Map等集合的泛型換成我們自定義個class,這也是可以解決的,請看案例


案例五:

泛型的使用

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public HashMap<String,Book> booksMap;  
  2.   
  3. public class Book{  
  4.     public int id;  
  5.     public String name;  
  6. }  


booksMap轉換成字符串和上面的案例是一樣的,但是booksMap的Json字符串換成booksMap的實例對象就有點不同了,因爲booksMap有自定義的泛型

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. HashMap<String, Book> booksMap = gson.fromJson(result, new TypeToken<HashMap<String, Book>>() { }.getType());  

如果什麼疑問,請到我的博客列表頁面,QQ或者郵件聯繫我!如有轉載請著名來自http://blog.csdn.net/qxs965266509

源代碼下載鏈接:http://download.csdn.net/detail/qxs965266509/8367689


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章