Google Gson的使用方法,實現Json結構的相互轉換
阿新 • • 發佈:2019-02-19
在Java開發中,有時需要儲存一個資料結構成字串,可能你會考慮用Json,但是當Json字串轉換成Java物件時,轉換成的是JsonObject,並不是你想要的Class型別的物件,操作起來就很不是愉悅,下面說的就可以解決了這種問題。
首先,需要把Google的Gson的Jar包匯入到專案中,這個匯入包的簡單步驟就不展示了,Gson的下載連結:http://download.csdn.NET/detail/qxs965266509/8367275
現在,我先自定義一個Class類
[java] view plain copy print?- publicclass Student {
- public
- public String nickName;
- publicint age;
- public ArrayList<String> books;
- public HashMap<String, String> booksMap;
- }
public class Student { public int id; public String nickName; public int age; public ArrayList<String> books; public HashMap<String, String> booksMap; }
案例一,案例二,案例三都是把Java的Class物件使用Gson轉換成Json的字串
案例一:僅包含基本資料型別的資料結構
[java] view plain copy print?- Gson gson = new Gson();
- Student student = new Student();
- student.id = 1;
- student.nickName = ”喬曉鬆”;
- student.age = 22;
- student.email = ”[email protected]”;
- Log.e(”MainActivity”, gson.toJson(student));
Gson gson = new Gson();
Student student = new Student();
student.id = 1;
student.nickName = "喬曉鬆";
student.age = 22;
student.email = "[email protected]";
Log.e("MainActivity", gson.toJson(student));
輸出結果是 :
[java] view plain copy print?- {“email”:“[email protected]”,“nickName”:“喬曉鬆”,“id”:1,“age”:22}
{"email":"[email protected]","nickName":"喬曉鬆","id":1,"age":22}
案例二:除了基本資料型別還包含了List集合
[java] view plain copy print?- Gson gson = new Gson();
- Student student = new Student();
- student.id = 1;
- student.nickName = ”喬曉鬆”;
- student.age = 22;
- student.email = ”[email protected]”;
- 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));
Gson gson = new Gson();
Student student = new Student();
student.id = 1;
student.nickName = "喬曉鬆";
student.age = 22;
student.email = "[email protected]";
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));
輸出結果是 :[html] view plain copy print?- {“books”:[“數學”,”語文”,”英語”,”物理”,”化學”,”生物”],”email”:”[email protected]”,”nickName”:”喬曉鬆”,”id”:1,”age”:22}
{"books":["數學","語文","英語","物理","化學","生物"],"email":"[email protected]","nickName":"喬曉鬆","id":1,"age":22}
案例三:除了基本資料型別還包含了List和Map集合
[java] view plain copy print?- Gson gson = new Gson();
- Student student = new Student();
- student.id = 1;
- student.nickName = ”喬曉鬆”;
- student.age = 22;
- student.email = ”[email protected]”;
- 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));
Gson gson = new Gson();
Student student = new Student();
student.id = 1;
student.nickName = "喬曉鬆";
student.age = 22;
student.email = "[email protected]";
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));
輸出結果是 :
[java] view plain copy print?- {“books”:[“數學”,“語文”,“英語”,“物理”,“化學”,“生物”],“booksMap”:{“3”:“英語”,“2”:“語文”,“1”:“數學”,“6”:“生物”,“5”:“化學”,“4”:“物理”},“email”:“[email protected]”,“nickName”:“喬曉鬆”,“id”:1,“age”:22}
{"books":["數學","語文","英語","物理","化學","生物"],"booksMap":{"3":"英語","2":"語文","1":"數學","6":"生物","5":"化學","4":"物理"},"email":"[email protected]","nickName":"喬曉鬆","id":1,"age":22}
案例四:把案例三輸出的字串使用Gson轉換成Student物件
[java] view plain copy print?- Gson gson = new Gson();
- Student student = new Student();
- student.id = 1;
- student.nickName = ”喬曉鬆”;
- student.age = 22;
- student.email = ”[email protected]”;
- 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());
Gson gson = new Gson();
Student student = new Student();
student.id = 1;
student.nickName = "喬曉鬆";
student.age = 22;
student.email = "[email protected]";
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());
輸出結果是 :
[html] view plain copy print?- id:1
- nickName:喬曉鬆
- age:22
- email:[email protected]
- books size:6
- booksMap size:6
id:1
nickName:喬曉鬆
age:22
email:[email protected]
books size:6
booksMap size:6
通過這4個案例我解決你一定就把Gson的基本用法學會了,當然我們的需求可能需要把List或者Map等集合的泛型換成我們自定義個class,這也是可以解決的,請看案例
案例五:泛型的使用
[html] view plain copy print?- public HashMap<String,Book> booksMap;
- public class Book{
- public int id;
- public String name;
- }
public HashMap<String,Book> booksMap;
public class Book{
public int id;
public String name;
}
把booksMap轉換成字串和上面的案例是一樣的,但是booksMap的Json字串換成booksMap的例項物件就有點不同了,因為booksMap有自定義的泛型
[html] view plain copy print?- HashMap<String, Book>booksMap = gson.fromJson(result, new TypeToken<HashMap<String, Book>>() { }.getType());