Google Gson的使用方法,實現Json結構的相互轉換
來源:程序員人生 發布時間:2015-02-27 07:54:55 閱讀次數:3234次
在Java開發中,有時需要保存1個數據結構成字符串,可能你會斟酌用Json,但是當Json字符串轉換成Java對象時,轉換成的是JsonObject,其實不是你想要的Class類型的對象,操作起來就很不是愉悅,下面說的就能夠解決了這類問題。
首先,需要把Google的Gson的Jar包導入到項目中,這個導入包的簡單步驟就不展現了,Gson的下載鏈接:http://download.csdn.net/detail/qxs965266509/8367275
現在,我先自定義1個Class類
public class Student {
public int id;
public String nickName;
public int age;
public ArrayList<String> books;
public HashMap<String, String> booksMap;
}
案例1,案例2,案例3都是把Java的Class對象使用Gson轉換成Json的字符串
案例1:
僅包括基本數據類型的數據結構
Gson gson = new Gson();
Student student = new Student();
student.id = 1;
student.nickName = "喬曉松";
student.age = 22;
student.email = "965266509@qq.com";
Log.e("MainActivity", gson.toJson(student));
輸出結果是 :
{"email":"965266509@qq.com","nickName":"喬曉松","id":1,"age":22}
案例2:
除基本數據類型還包括了List集合
Gson gson = new Gson();
Student student = new Student();
student.id = 1;
student.nickName = "喬曉松";
student.age = 22;
student.email = "965266509@qq.com";
ArrayList<String> books = new ArrayList<String>();
books.add("數學");
books.add("語文");
books.add("英語");
books.add("物理");
books.add("化學");
books.add("生物");
student.books = books;
Log.e("MainActivity", gson.toJson(student));
輸出結果是 :{"books":["數學","語文","英語","物理","化學","生物"],"email":"965266509@qq.com","nickName":"喬曉松","id":1,"age":22}
案例3:
除基本數據類型還包括了List和Map集合
Gson gson = new Gson();
Student student = new Student();
student.id = 1;
student.nickName = "喬曉松";
student.age = 22;
student.email = "965266509@qq.com";
ArrayList<String> books = new ArrayList<String>();
books.add("數學");
books.add("語文");
books.add("英語");
books.add("物理");
books.add("化學");
books.add("生物");
student.books = books;
HashMap<String, String> booksMap = new HashMap<String, String>();
booksMap.put("1", "數學");
booksMap.put("2", "語文");
booksMap.put("3", "英語");
booksMap.put("4", "物理");
booksMap.put("5", "化學");
booksMap.put("6", "生物");
student.booksMap = booksMap;
Log.e("MainActivity", gson.toJson(student));
輸出結果是 :
{"books":["數學","語文","英語","物理","化學","生物"],"booksMap":{"3":"英語","2":"語文","1":"數學","6":"生物","5":"化學","4":"物理"},"email":"965266509@qq.com","nickName":"喬曉松","id":1,"age":22}
案例4:
把案例3輸出的字符串使用Gson轉換成Student對象
Gson gson = new Gson();
Student student = new Student();
student.id = 1;
student.nickName = "喬曉松";
student.age = 22;
student.email = "965266509@qq.com";
ArrayList<String> books = new ArrayList<String>();
books.add("數學");
books.add("語文");
books.add("英語");
books.add("物理");
books.add("化學");
books.add("生物");
student.books = books;
HashMap<String, String> booksMap = new HashMap<String, String>();
booksMap.put("1", "數學");
booksMap.put("2", "語文");
booksMap.put("3", "英語");
booksMap.put("4", "物理");
booksMap.put("5", "化學");
booksMap.put("6", "生物");
student.booksMap = booksMap;
String result = gson.toJson(student);
Student studentG = gson.fromJson(result, Student.class);
Log.e("MainActivity", "id:" + studentG.id);
Log.e("MainActivity", "nickName:" + studentG.nickName);
Log.e("MainActivity", "age:" + studentG.age);
Log.e("MainActivity", "email:" + studentG.email);
Log.e("MainActivity", "books size:" + studentG.books.size());
Log.e("MainActivity", "booksMap size:" + studentG.booksMap.size());
輸出結果是 :
id:1
nickName:喬曉松
age:22
email:965266509@qq.com
books size:6
booksMap size:6
通過這4個案例我解決你1定就把Gson的基本用法學會了,固然我們的需求可能需要把List或Map等集合的泛型換成我們自定義個class,這也是可以解決的,請看案例
案例5:
泛型的使用
public HashMap<String,Book> booksMap;
public class Book{
public int id;
public String name;
}
把booksMap轉換成字符串和上面的案例是1樣的,但是booksMap的Json字符串換成booksMap的實例對象就有點不同了,由于booksMap有自定義的泛型
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
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈