Google Gson的使用方法,实现Json结构的相互转换
来源:程序员人生 发布时间:2015-02-27 07:54:55 阅读次数:3041次
在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
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠