美文网首页
Gson的使用方法

Gson的使用方法

作者: 毕丙伟 | 来源:发表于2017-10-29 11:01 被阅读0次
    Gson是一个可用于将Java对象转换为JSON表示形式的Java库。也可用于将JSON字符串转换为等效的Java对象。

    Gson目标

    1. 提供简单的toJson()和fromJson()方法将Java对象转换为JSON,反之亦然.
    2. 允许将先前存在的不可修改对象转换为JSON或从JSON转换。
    3. 支持Java泛型
    4. 允许对象的自定义表示
    5. 支持任意复杂的对象

    以上内容来自官网

    现在,我们自定义一个class类

    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的字符串

    1. 包含基本数据类型的数据结构
    Gson gson = new Gson();  
        Student student = new Student();  
        student.id = 1;  
        student.nickName = "乔晓松";  
        student.age = 22;    
        Log.e("MainActivity", gson.toJson(student)); 
    

    输出结果:

    {"nickName":"乔晓松","id":1,"age":22} 
    
    1. 包含List集合
    Gson gson = new Gson();  
            Student student = new Student();  
            student.id = 1;  
            student.nickName = "乔晓松";  
            student.age = 22;   
            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":["数学","语文","英语","物理","化学","生物"],"nickName":"乔晓松","id":1,"age":22}  
    
    1. List和Map集合同时存在
    Gson gson = new Gson();  
            Student student = new Student();  
            student.id = 1;  
            student.nickName = "乔晓松";  
            student.age = 22;  
            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":"物理"},"nickName":"乔晓松","id":1,"age":22}  
    
    1. 使用Gson,将字符串转换为Student对象
    Gson gson = new Gson();  
            Student student = new Student();  
            student.id = 1;  
            student.nickName = "乔晓松";  
            student.age = 22;  
            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", "books size:" + studentG.books.size());  
            Log.e("MainActivity", "booksMap size:" + studentG.booksMap.size());  
    

    输出结果:

    id:1  
    nickName:乔晓松  
    age:22  
    books size:6  
    booksMap size:6  
    
    1. 泛型的使用
    public HashMap<String,Book> booksMap;  
      
    public class Book{  
        public int id;  
        public String name;  
    }  
    

    把booksMap转换成字符串和上面的举例一样,但是booksMap的Json字符串转换成booksMap的实例对象就有点不同,因为booksMap有自定义的房型

    HashMap<String, Book> booksMap = gson.fromJson(result, new TypeToken<HashMap<String, Book>>() { }.getType());  
    

    参考:http://blog.csdn.net/qxs965266509/article/details/42774691

    相关文章

      网友评论

          本文标题:Gson的使用方法

          本文链接:https://www.haomeiwen.com/subject/mctypxtx.html