Gson 解析教程

Gson 是google解析Json的一个开源框架,同类的框架fastJson,JackJson等等

本人fastJson用了两年,也是从去年才开始接触Gson,希望下面的总结会对博友有用,至于Gson与FastJson的对比,其实半斤八两的问题,不再赘述

第一步:在AS下面创建Java工程 并添加gson依赖

compile 'com.google.code.gson:gson:2.6.2'



Gson的实例化方式:

1:Gson gson=newGson();

2:通过GsonBuilder 可以配置多种选项

  1. gson = new GsonBuilder()  
  2.                    .setLenient()// json宽松  
  3.                    .enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式  
  4.                    .serializeNulls() //智能null  
  5.                    .setPrettyPrinting()// 调教格式  
  6.                    .disableHtmlEscaping() //默认是GSON把HTML 转义的  
  7.                    .create();  
 gson = new GsonBuilder()
                    .setLenient()// json宽松
                    .enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式
                    .serializeNulls() //智能null
                    .setPrettyPrinting()// 调教格式
                    .disableHtmlEscaping() //默认是GSON把HTML 转义的
                    .create();
Gson的基本用法:

 JavaBean转换Json字符串

 gson提供 publicString toJson(Object src) 方法可以将对象转换成jsonStr

  

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4.   
  5. /** 
  6.  * @author xuanyouwu 
  7.  * @email xuanyouwu@163.com 
  8.  * @time 2016-05-18 10:39 
  9.  */  
  10. public class GsonTest1 {  
  11.     public static class Student {  
  12.         private String name;  
  13.         private int age;  
  14.   
  15.         //省略setter getter equals  
  16.     }  
  17.   
  18.     private static void log(String msg) {  
  19.         System.out.println(msg);  
  20.     }  
  21.   
  22.   
  23.     public static void main(String[] args) throws Exception {  
  24.         Gson gson = new Gson();  
  25.         Student student = new Student();  
  26.         student.setName("xuanyouwu");  
  27.         student.setAge(26);  
  28.         String jsonStr = gson.toJson(student);  
  29.         log("---->javabean convert jsonStr:" + jsonStr);  
  30.     }  
  31.   
  32. }  
package com.xuan.gson;

import com.google.gson.Gson;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 10:39
 */
public class GsonTest1 {
    public static class Student {
        private String name;
        private int age;

        //省略setter getter equals
    }

    private static void log(String msg) {
        System.out.println(msg);
    }


    public static void main(String[] args) throws Exception {
        Gson gson = new Gson();
        Student student = new Student();
        student.setName("xuanyouwu");
        student.setAge(26);
        String jsonStr = gson.toJson(student);
        log("---->javabean convert jsonStr:" + jsonStr);
    }

}

运行结果:

---->javabean convert jsonStr:{"name":"xuanyouwu","age":26}




    List Map转Json字符串

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4.   
  5. import java.util.Arrays;  
  6. import java.util.HashMap;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. /** 
  11.  * @author xuanyouwu 
  12.  * @email xuanyouwu@163.com 
  13.  * @time 2016-05-18 10:39 
  14.  */  
  15. public class GsonTest1 {  
  16.   
  17.     private static void log(String msg) {  
  18.         System.out.println(msg);  
  19.     }  
  20.   
  21.   
  22.     public static void main(String[] args) throws Exception {  
  23.         Gson gson = new Gson();  
  24.   
  25.         List<String> list = Arrays.asList("1""a""3""rt""5");  
  26.         log("---->list convert jsonStr:" + gson.toJson(list));  
  27.   
  28.         Map<String, Object> content = new HashMap<String, Object>();  
  29.         content.put("name""xuanyouwu");  
  30.         content.put("age""26");  
  31.         log("---->map convert jsonStr:" + gson.toJson(content));  
  32.     }  
  33.   
  34. }  
package com.xuan.gson;

import com.google.gson.Gson;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 10:39
 */
public class GsonTest1 {

    private static void log(String msg) {
        System.out.println(msg);
    }


    public static void main(String[] args) throws Exception {
        Gson gson = new Gson();

        List<String> list = Arrays.asList("1", "a", "3", "rt", "5");
        log("---->list convert jsonStr:" + gson.toJson(list));

        Map<String, Object> content = new HashMap<String, Object>();
        content.put("name", "xuanyouwu");
        content.put("age", "26");
        log("---->map convert jsonStr:" + gson.toJson(content));
    }

}
  1. 运行结果:  
运行结果:
  1. ---->list convert jsonStr:["1","a","3","rt","5"]  
  2. ---->map convert jsonStr:{"name":"xuanyouwu","age":"26"}  
---->list convert jsonStr:["1","a","3","rt","5"]
---->map convert jsonStr:{"name":"xuanyouwu","age":"26"}

       Json字符串转JavaBean

  1. String studentJsonStr="{\"name\":\"xuanyouwu\",\"age\":26}";  
  2.   
  3. Student student1 = gson.fromJson(studentJsonStr, Student.class);  
  4. log("------->json convert JavaBean:"+student1);  
        String studentJsonStr="{\"name\":\"xuanyouwu\",\"age\":26}";

        Student student1 = gson.fromJson(studentJsonStr, Student.class);
        log("------->json convert JavaBean:"+student1);

      运行结果:

            ------->json convert JavaBean:Student{name='xuanyouwu', age=26}



   Json字符串转List

  1. String listJsonStr="[\"1\",\"a\",\"3\",\"rt\",\"5\"]";  
        String listJsonStr="[\"1\",\"a\",\"3\",\"rt\",\"5\"]";
  1. Type type = new TypeToken<ArrayList<String>>() {  
  2. }.getType();  
  3. ArrayList<String> sList=gson.fromJson(listJsonStr, type);  
  4. log("------->json convert List:"+sList);  
        Type type = new TypeToken<ArrayList<String>>() {
        }.getType();
        ArrayList<String> sList=gson.fromJson(listJsonStr, type);
        log("------->json convert List:"+sList);
  1. 运行结果:  
    运行结果:
  1. ------->json convert List:[1, a, 3, rt, 5]  
        ------->json convert List:[1, a, 3, rt, 5]
  1. </pre><pre code_snippet_id="1687762" snippet_file_name="blog_20160518_11_939636" name="code" class="java">  Gson封装的类型体系  
</pre><pre code_snippet_id="1687762" snippet_file_name="blog_20160518_11_939636" name="code" class="java">  Gson封装的类型体系
  1.    Gson里面有一个非常有意思的抽象基类JsonElement,他的继承体系:  
    Gson里面有一个非常有意思的抽象基类JsonElement,他的继承体系:
  1. <img src="https://img-blog.csdn.net/20160518135136864?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
    <img src="https://img-blog.csdn.net/20160518135136864?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
  1.  JsonObject等同于org的JSONObject,JsonArray也类似,  
 JsonObject等同于org的JSONObject,JsonArray也类似,
  1. JsonNull 其实就是null 字段  
   JsonNull 其实就是null 字段
  1. JsonNull jsonNull=new JsonNull();//构造方法过时,推荐INSTANCE  
     JsonNull jsonNull=new JsonNull();//构造方法过时,推荐INSTANCE

 
 
  1. JsonNull jsonNull=JsonNull.INSTANCE;  
  2.   log("-----"+jsonNull);  
      JsonNull jsonNull=JsonNull.INSTANCE;
        log("-----"+jsonNull);
  1. 运行结果:  
   运行结果:
  1. -----null  
      -----null

     JsonPrimitive非常有意思,我们知道如果json转换成字符串 可能包含引号的转义,但是通过JsonPrimative我们可以获得为转义的字符串,看实例:

     

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.JsonNull;  
  4. import com.google.gson.JsonPrimitive;  
  5.   
  6. /** 
  7.  * @author xuanyouwu 
  8.  * @email xuanyouwu@163.com 
  9.  * @time 2016-05-18 11:20 
  10.  */  
  11. public class GsonTest2 {  
  12.     private static void log(String msg) {  
  13.         System.out.println(msg);  
  14.     }  
  15.     public static void main(String[] args) throws Exception {  
  16.         String studentJsonStr="{\"name\":\"xuanyouwu\",\"age\":26}";  
  17.         log("------>studentJsonStr:"+studentJsonStr);  
  18.         JsonPrimitive jsonPrimitive=new JsonPrimitive(studentJsonStr);  
  19.         log("------>jsonPrimitive:"+jsonPrimitive);  
  20.         log("------>jsonPrimitive:"+jsonPrimitive.toString());  
  21.         log("------>jsonPrimitive:"+jsonPrimitive.getAsString());  
  22.   
  23.         JsonPrimitive jsonPrimitive2=new JsonPrimitive("this is String");  
  24.         log("------>jsonPrimitive2:"+jsonPrimitive2);  
  25.         log("------>jsonPrimitive2:"+jsonPrimitive2.toString());  
  26.         log("------>jsonPrimitive2:"+jsonPrimitive2.getAsString());  
  27.     }  
  28. }  
package com.xuan.gson;

import com.google.gson.JsonNull;
import com.google.gson.JsonPrimitive;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest2 {
    private static void log(String msg) {
        System.out.println(msg);
    }
    public static void main(String[] args) throws Exception {
        String studentJsonStr="{\"name\":\"xuanyouwu\",\"age\":26}";
        log("------>studentJsonStr:"+studentJsonStr);
        JsonPrimitive jsonPrimitive=new JsonPrimitive(studentJsonStr);
        log("------>jsonPrimitive:"+jsonPrimitive);
        log("------>jsonPrimitive:"+jsonPrimitive.toString());
        log("------>jsonPrimitive:"+jsonPrimitive.getAsString());

        JsonPrimitive jsonPrimitive2=new JsonPrimitive("this is String");
        log("------>jsonPrimitive2:"+jsonPrimitive2);
        log("------>jsonPrimitive2:"+jsonPrimitive2.toString());
        log("------>jsonPrimitive2:"+jsonPrimitive2.getAsString());
    }
}

     运行结果:

------>studentJsonStr:{"name":"xuanyouwu","age":26}
------>jsonPrimitive:"{\"name\":\"xuanyouwu\",\"age\":26}"
------>jsonPrimitive:"{\"name\":\"xuanyouwu\",\"age\":26}"
------>jsonPrimitive:{"name":"xuanyouwu","age":26}
------>jsonPrimitive2:"this is String"
------>jsonPrimitive2:"this is String"
------>jsonPrimitive2:this is String




       创建JsonObject

           通过addPropert(key,value)可以向jsonObject中添加字段 跟hashMap类似

  1. JsonObject jsonObject=new JsonObject();  
  2. jsonObject.addProperty("name","xuanyouwu");  
  3. jsonObject.addProperty("age",26);  
  4. log("------>create jsonObject:"+jsonObject);  
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("name","xuanyouwu");
        jsonObject.addProperty("age",26);
        log("------>create jsonObject:"+jsonObject);
            运行结果:

                ------>create jsonObject:{"name":"xuanyouwu","age":26}



    创建JsonArray       

  1. JsonArray jsonElements=new JsonArray();  
  2.    jsonElements.add("a");  
  3.    jsonElements.add("b");  
  4.    jsonElements.add("c");  
  5.    jsonElements.add("d");  
  6.    log("------>create jsonArray:"+jsonElements);  
     JsonArray jsonElements=new JsonArray();
        jsonElements.add("a");
        jsonElements.add("b");
        jsonElements.add("c");
        jsonElements.add("d");
        log("------>create jsonArray:"+jsonElements);
          运行结果:

          ------>create jsonArray:["a","b","c","d"]



     JsonObject 嵌套数组或者说嵌套JsonArray

           通过JsonObject的add(key,JsonElement)可以为jsonObject 添加一个数组的字段

  1. JsonObject jsonObject2=new JsonObject();  
  2. jsonObject2.addProperty("name","xuanyouwu");  
  3. jsonObject2.addProperty("age",26);  
  4. JsonArray jsonElements2=new JsonArray();  
  5. jsonElements2.add("骑车");  
  6. jsonElements2.add("打游戏");  
  7. jsonElements2.add("看电视");  
  8. jsonObject2.add("hobby",jsonElements2);  
  9. log("------>create jsonObject inner JsonArray:"+jsonObject2);  
        JsonObject jsonObject2=new JsonObject();
        jsonObject2.addProperty("name","xuanyouwu");
        jsonObject2.addProperty("age",26);
        JsonArray jsonElements2=new JsonArray();
        jsonElements2.add("骑车");
        jsonElements2.add("打游戏");
        jsonElements2.add("看电视");
        jsonObject2.add("hobby",jsonElements2);
        log("------>create jsonObject inner JsonArray:"+jsonObject2);
           运行结果:

            ------>create jsonObject inner JsonArray:{"name":"xuanyouwu","age":26,"hobby":["骑车","打游戏","看电视"]}




Gson注解

          在Gson中有五类注解

          




          重命名注解:SerializedName 

             作用:转换关键字key,json转换成JavaBean时,json字段的key 默认必须和我们声明类的字段名称一样,当服务器端返回了关键字怎么办,比如key 为new switch这样,我们    在声明类的时候不能写这样的字段,可能你想服务器端改动,他可能要改数据库,但是我告诉你,做服务端的大部分不愿意改动他的json,是很自私的!这时候重命名注解都排上用场了   第二种场景:服务器端返回的json 的key 简直太丑,或者太长,你想简化,my_parent_name,可以简化成mpn 比较优雅简介

          实例:

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.annotations.SerializedName;  
  5.   
  6. /** 
  7.  * @author xuanyouwu 
  8.  * @email xuanyouwu@163.com 
  9.  * @time 2016-05-18 11:20 
  10.  */  
  11. public class GsonTest3 {  
  12.     private static void log(String msg) {  
  13.         System.out.println(msg);  
  14.     }  
  15.   
  16.     public static class User {  
  17.         public String name;  
  18.         public int age;  
  19.         @SerializedName("new")  
  20.         public int isnew;  
  21.   
  22.         @Override  
  23.         public String toString() {  
  24.             return "User{" +  
  25.                     "name='" + name + '\'' +  
  26.                     ", age=" + age +  
  27.                     ", isnew=" + isnew +  
  28.                     '}';  
  29.         }  
  30.     }  
  31.   
  32.     public static void main(String[] args) throws Exception {  
  33.   
  34.         String jsonFromServer = "{\n" +  
  35.                 "    \"age\": 26,\n" +  
  36.                 "    \"name\": \"zhangsan\",\n" +  
  37.                 "    \"new\": 1\n" +  
  38.                 "}";  
  39.         Gson gson = new Gson();  
  40.         User user = gson.fromJson(jsonFromServer, User.class);  
  41.         log("------>user:" + user);  
  42.     }  
  43. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest3 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        public String name;
        public int age;
        @SerializedName("new")
        public int isnew;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", isnew=" + isnew +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {

        String jsonFromServer = "{\n" +
                "    \"age\": 26,\n" +
                "    \"name\": \"zhangsan\",\n" +
                "    \"new\": 1\n" +
                "}";
        Gson gson = new Gson();
        User user = gson.fromJson(jsonFromServer, User.class);
        log("------>user:" + user);
    }
}

运行结果: ------>user:User{name='zhangsan', age=26, isnew=1}


  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.annotations.SerializedName;  
  5.   
  6. /** 
  7.  * @author xuanyouwu 
  8.  * @email xuanyouwu@163.com 
  9.  * @time 2016-05-18 11:20 
  10.  */  
  11. public class GsonTest3 {  
  12.     private static void log(String msg) {  
  13.         System.out.println(msg);  
  14.     }  
  15.   
  16.     public static class User2 {  
  17.         public String name;  
  18.         public int age;  
  19.         @SerializedName("my_parent_name")  
  20.         public String pn;  
  21.   
  22.         @Override  
  23.         public String toString() {  
  24.             return "User2{" +  
  25.                     "name='" + name + '\'' +  
  26.                     ", age=" + age +  
  27.                     ", pn='" + pn + '\'' +  
  28.                     '}';  
  29.         }  
  30.     }  
  31.   
  32.     public static void main(String[] args) throws Exception {  
  33.         String jsonFromServer2="{\n" +  
  34.                 "    \"age\": 26,\n" +  
  35.                 "    \"my_parent_name\": \"zhangsanf\",\n" +  
  36.                 "    \"name\": \"zhangsan\"\n" +  
  37.                 "}";  
  38.         Gson gson2 = new Gson();  
  39.         User2 user2 = gson2.fromJson(jsonFromServer2, User2.class);  
  40.         log("------>user2:" + user2);  
  41.     }  
  42. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest3 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User2 {
        public String name;
        public int age;
        @SerializedName("my_parent_name")
        public String pn;

        @Override
        public String toString() {
            return "User2{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", pn='" + pn + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        String jsonFromServer2="{\n" +
                "    \"age\": 26,\n" +
                "    \"my_parent_name\": \"zhangsanf\",\n" +
                "    \"name\": \"zhangsan\"\n" +
                "}";
        Gson gson2 = new Gson();
        User2 user2 = gson2.fromJson(jsonFromServer2, User2.class);
        log("------>user2:" + user2);
    }
}

运行结果: ------>user2:User2{name='zhangsan', age=26, pn='zhangsanf'}


        作用2:结合alternate 提供多种备用字段key来解析,@SerializedName(value ="desc",alternate = {"other","note"})  如果json中有other就会解析成desc 如果有note也会解析成desc,注意1:value中的值不能出现在alternate中,注意2:alternate的备选字段 会后面的替换前面的

   实例:

   

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.annotations.SerializedName;  
  5.   
  6. /** 
  7.  * @author xuanyouwu 
  8.  * @email xuanyouwu@163.com 
  9.  * @time 2016-05-18 11:20 
  10.  */  
  11. public class GsonTest4 {  
  12.     private static void log(String msg) {  
  13.         System.out.println(msg);  
  14.     }  
  15.   
  16.     public static class User {  
  17.         public String name;  
  18.         public int age;  
  19.         @SerializedName(value = "desc",alternate = {"other","note"})  
  20.         public String desc;  
  21.   
  22.         @Override  
  23.         public String toString() {  
  24.             return "User{" +  
  25.                     "name='" + name + '\'' +  
  26.                     ", age=" + age +  
  27.                     ", desc='" + desc + '\'' +  
  28.                     '}';  
  29.         }  
  30.     }  
  31.   
  32.     public static void main(String[] args) throws Exception {  
  33.         String jsonFromServer = "{\n" +  
  34.                 "    \"age\": 26,\n" +  
  35.                 "    \"other\": \"成都人\",\n" +  
  36.                 "    \"name\": \"zhangsan\"\n" +  
  37.                 "}";  
  38.         Gson gson = new Gson();  
  39.         User user = gson.fromJson(jsonFromServer, User.class);  
  40.         log("------>user:" + user);  
  41.   
  42.   
  43.         String jsonFromServer2 = "{\n" +  
  44.                 "    \"age\": 26,\n" +  
  45.                 "    \"note\": \"成都人\",\n" +  
  46.                 "    \"name\": \"zhangsan\"\n" +  
  47.                 "}";  
  48.         User user2 = gson.fromJson(jsonFromServer2, User.class);  
  49.         log("------>user:" + user2);  
  50.   
  51.         //包括desc 与note note在desc之后  
  52.         String jsonFromServer3="{\n" +  
  53.                 "    \"age\": 26,\n" +  
  54.                 "    \"desc\": \"desc成都人\",\n" +  
  55.                 "    \"name\": \"zhangsan\",\n" +  
  56.                 "    \"note\": \"note成都人\"\n" +  
  57.                 "}";  
  58.         User user3 = gson.fromJson(jsonFromServer3, User.class);  
  59.         log("------>user:" + user3);  
  60.   
  61.         //包括desc 与note note在desc之前  
  62.         String jsonFromServer4="{\n" +  
  63.                 "    \"age\": 26,\n" +  
  64.                 "    \"note\": \"note成都人\",\n" +  
  65.                 "    \"name\": \"zhangsan\",\n" +  
  66.                 "    \"desc\": \"desc成都人\"\n" +  
  67.                 "}";  
  68.         User user4 = gson.fromJson(jsonFromServer4, User.class);  
  69.         log("------>user:" + user4);  
  70.     }  
  71. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest4 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        public String name;
        public int age;
        @SerializedName(value = "desc",alternate = {"other","note"})
        public String desc;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", desc='" + desc + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        String jsonFromServer = "{\n" +
                "    \"age\": 26,\n" +
                "    \"other\": \"成都人\",\n" +
                "    \"name\": \"zhangsan\"\n" +
                "}";
        Gson gson = new Gson();
        User user = gson.fromJson(jsonFromServer, User.class);
        log("------>user:" + user);


        String jsonFromServer2 = "{\n" +
                "    \"age\": 26,\n" +
                "    \"note\": \"成都人\",\n" +
                "    \"name\": \"zhangsan\"\n" +
                "}";
        User user2 = gson.fromJson(jsonFromServer2, User.class);
        log("------>user:" + user2);

        //包括desc 与note note在desc之后
        String jsonFromServer3="{\n" +
                "    \"age\": 26,\n" +
                "    \"desc\": \"desc成都人\",\n" +
                "    \"name\": \"zhangsan\",\n" +
                "    \"note\": \"note成都人\"\n" +
                "}";
        User user3 = gson.fromJson(jsonFromServer3, User.class);
        log("------>user:" + user3);

        //包括desc 与note note在desc之前
        String jsonFromServer4="{\n" +
                "    \"age\": 26,\n" +
                "    \"note\": \"note成都人\",\n" +
                "    \"name\": \"zhangsan\",\n" +
                "    \"desc\": \"desc成都人\"\n" +
                "}";
        User user4 = gson.fromJson(jsonFromServer4, User.class);
        log("------>user:" + user4);
    }
}
  运行结果:

     ------>user:User{name='zhangsan', age=26, desc='成都人'}
------>user:User{name='zhangsan', age=26, desc='成都人'}
------>user:User{name='zhangsan', age=26, desc='note成都人'}
------>user:User{name='zhangsan', age=26, desc='desc成都人'}



  Gson @Expose过滤注解 

  源码:默认既可以序列化又可以反序列化

  1. @Retention(RetentionPolicy.RUNTIME)  
  2. @Target(ElementType.FIELD)  
  3. public @interface Expose {  
  4.     
  5.   public boolean serialize() default true;  
  6.   
  7.   public boolean deserialize() default true;  
  8. }  
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Expose {
  
  public boolean serialize() default true;

  public boolean deserialize() default true;
}
  1. </pre><pre code_snippet_id="1687762" snippet_file_name="blog_20160518_25_4965554" name="code" class="java">可以排除不需要序列化的字段,需要配合GsonBuilder使用  
</pre><pre code_snippet_id="1687762" snippet_file_name="blog_20160518_25_4965554" name="code" class="java">可以排除不需要序列化的字段,需要配合GsonBuilder使用
  1. <pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java"> Gson gson = new GsonBuilder()  
  2.                 .excludeFieldsWithoutExposeAnnotation()  
  3.                 .create();  
<pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java"> Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .create();

 
 
 
 

  不添加@Expose注解的字段将不会解析:
   分为以下几种情况:
    1:不添加@Expose注解等同于@Expose(deserialize = false,serialize = false) 不做任何解析
    2:@Expose(deserialize = true,serialize = false) 只解析用用,也就是反序列化可以,序列化不可以
    3:@Expose(deserialize = false,serialize = true) 序列化可以,反序列化不行
    4:@Expose(deserialize = true,serialize = true) 既可以序列化,也可以反序列化
    实例:将分别演示这四种情况



  不添加@Expose注解:等同于@Expose(deserialize = false,serialize = false)

  
  
  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.GsonBuilder;  
  5.   
  6. /** 
  7.  * @author xuanyouwu 
  8.  * @email xuanyouwu@163.com 
  9.  * @time 2016-05-18 11:20 
  10.  */  
  11. public class GsonTest5 {  
  12.     private static void log(String msg) {  
  13.         System.out.println(msg);  
  14.     }  
  15.   
  16.     public static class User {  
  17.         public String name;  
  18.   
  19.         @Override  
  20.         public String toString() {  
  21.             return "User{" +  
  22.                     "name='" + name + '\'' +  
  23.                     '}';  
  24.         }  
  25.     }  
  26.   
  27.     public static void main(String[] args) throws Exception {  
  28.         String jsonFromServer = "{\"name\": \"zhangsan\"}";  
  29.         Gson gson = new GsonBuilder()  
  30.                 .excludeFieldsWithoutExposeAnnotation()  
  31.                 .create();  
  32.         User user = gson.fromJson(jsonFromServer, User.class);  
  33.         log("------>反序列化:" + user);  
  34.   
  35.         User user1 = new User();  
  36.         user1.name = "zhangsan2";  
  37.         String userStr = gson.toJson(user1);  
  38.         log("------>序列化:" + userStr);  
  39.     }  
  40. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest5 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        public String name;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        String jsonFromServer = "{\"name\": \"zhangsan\"}";
        Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .create();
        User user = gson.fromJson(jsonFromServer, User.class);
        log("------>反序列化:" + user);

        User user1 = new User();
        user1.name = "zhangsan2";
        String userStr = gson.toJson(user1);
        log("------>序列化:" + userStr);
    }
}

运行结果:
------>反序列化:User{name='null'}
------>序列化:{}
 
 
 
 
 
 
 
 
 
 
  添加@Expose注解 
  
  
  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.GsonBuilder;  
  5. import com.google.gson.annotations.Expose;  
  6.   
  7. /** 
  8.  * @author xuanyouwu 
  9.  * @email xuanyouwu@163.com 
  10.  * @time 2016-05-18 11:20 
  11.  */  
  12. public class GsonTest5 {  
  13.     private static void log(String msg) {  
  14.         System.out.println(msg);  
  15.     }  
  16.   
  17.     public static class User {  
  18.         @Expose //等同于 @Expose(deserialize = true,serialize = true)  
  19.         public String name;  
  20.   
  21.         @Override  
  22.         public String toString() {  
  23.             return "User{" +  
  24.                     "name='" + name + '\'' +  
  25.                     '}';  
  26.         }  
  27.     }  
  28.   
  29.     public static void main(String[] args) throws Exception {  
  30.         String jsonFromServer = "{\"name\": \"zhangsan\"}";  
  31.         Gson gson = new GsonBuilder()  
  32.                 .excludeFieldsWithoutExposeAnnotation()  
  33.                 .create();  
  34.         User user = gson.fromJson(jsonFromServer, User.class);  
  35.         log("------>反序列化:" + user);  
  36.   
  37.         User user1 = new User();  
  38.         user1.name = "zhangsan2";  
  39.         String userStr = gson.toJson(user1);  
  40.         log("------>序列化:" + userStr);  
  41.     }  
  42. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest5 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        @Expose //等同于 @Expose(deserialize = true,serialize = true)
        public String name;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        String jsonFromServer = "{\"name\": \"zhangsan\"}";
        Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .create();
        User user = gson.fromJson(jsonFromServer, User.class);
        log("------>反序列化:" + user);

        User user1 = new User();
        user1.name = "zhangsan2";
        String userStr = gson.toJson(user1);
        log("------>序列化:" + userStr);
    }
}

运行结果:

------>反序列化:User{name='zhangsan'}
------>序列化:{"name":"zhangsan2"}



@Expose注解 只序列化

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.GsonBuilder;  
  5. import com.google.gson.annotations.Expose;  
  6.   
  7. /** 
  8.  * @author xuanyouwu 
  9.  * @email xuanyouwu@163.com 
  10.  * @time 2016-05-18 11:20 
  11.  */  
  12. public class GsonTest5 {  
  13.     private static void log(String msg) {  
  14.         System.out.println(msg);  
  15.     }  
  16.   
  17.     public static class User {  
  18.         @Expose(deserialize = false,serialize = true)  
  19.         public String name;  
  20.   
  21.         @Override  
  22.         public String toString() {  
  23.             return "User{" +  
  24.                     "name='" + name + '\'' +  
  25.                     '}';  
  26.         }  
  27.     }  
  28.   
  29.     public static void main(String[] args) throws Exception {  
  30.         String jsonFromServer = "{\"name\": \"zhangsan\"}";  
  31.         Gson gson = new GsonBuilder()  
  32.                 .excludeFieldsWithoutExposeAnnotation()  
  33.                 .create();  
  34.         User user = gson.fromJson(jsonFromServer, User.class);  
  35.         log("------>反序列化:" + user);  
  36.   
  37.         User user1 = new User();  
  38.         user1.name = "zhangsan2";  
  39.         String userStr = gson.toJson(user1);  
  40.         log("------>序列化:" + userStr);  
  41.     }  
  42. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest5 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        @Expose(deserialize = false,serialize = true)
        public String name;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        String jsonFromServer = "{\"name\": \"zhangsan\"}";
        Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .create();
        User user = gson.fromJson(jsonFromServer, User.class);
        log("------>反序列化:" + user);

        User user1 = new User();
        user1.name = "zhangsan2";
        String userStr = gson.toJson(user1);
        log("------>序列化:" + userStr);
    }
}

 
 运行结果: 
 

------>反序列化:User{name='null'}------>序列化:{"name":"zhangsan2"}

      @Expose 只反序列化

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.GsonBuilder;  
  5. import com.google.gson.annotations.Expose;  
  6.   
  7. /** 
  8.  * @author xuanyouwu 
  9.  * @email xuanyouwu@163.com 
  10.  * @time 2016-05-18 11:20 
  11.  */  
  12. public class GsonTest5 {  
  13.     private static void log(String msg) {  
  14.         System.out.println(msg);  
  15.     }  
  16.   
  17.     public static class User {  
  18.         @Expose(deserialize = true, serialize = false)  
  19.         public String name;  
  20.   
  21.         @Override  
  22.         public String toString() {  
  23.             return "User{" +  
  24.                     "name='" + name + '\'' +  
  25.                     '}';  
  26.         }  
  27.     }  
  28.   
  29.     public static void main(String[] args) throws Exception {  
  30.         String jsonFromServer = "{\"name\": \"zhangsan\"}";  
  31.         Gson gson = new GsonBuilder()  
  32.                 .excludeFieldsWithoutExposeAnnotation()  
  33.                 .create();  
  34.         User user = gson.fromJson(jsonFromServer, User.class);  
  35.         log("------>反序列化:" + user);  
  36.   
  37.         User user1 = new User();  
  38.         user1.name = "zhangsan2";  
  39.         String userStr = gson.toJson(user1);  
  40.         log("------>序列化:" + userStr);  
  41.     }  
  42. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest5 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        @Expose(deserialize = true, serialize = false)
        public String name;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        String jsonFromServer = "{\"name\": \"zhangsan\"}";
        Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .create();
        User user = gson.fromJson(jsonFromServer, User.class);
        log("------>反序列化:" + user);

        User user1 = new User();
        user1.name = "zhangsan2";
        String userStr = gson.toJson(user1);
        log("------>序列化:" + userStr);
    }
}

运行结果:

 ------>反序列化:User{name='zhangsan'}
------>序列化:{}



    @Since(float v)注解  版本控制 

      结合GsonBuilder.setVersion(n)使用 当n>=v时 才会序列化解析

 

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.GsonBuilder;  
  5. import com.google.gson.annotations.Since;  
  6.   
  7. /** 
  8.  * @author xuanyouwu 
  9.  * @email xuanyouwu@163.com 
  10.  * @time 2016-05-18 11:20 
  11.  */  
  12. public class GsonTest6 {  
  13.     private static void log(String msg) {  
  14.         System.out.println(msg);  
  15.     }  
  16.   
  17.     public static class User {  
  18.         @Since(2)  
  19.         public String name;  
  20.   
  21.         @Override  
  22.         public String toString() {  
  23.             return "User{" +  
  24.                     "name='" + name + '\'' +  
  25.                     '}';  
  26.         }  
  27.     }  
  28.   
  29.     public static void main(String[] args) throws Exception {  
  30.         String jsonFromServer = "{\"name\": \"zhangsan\"}";  
  31.         Gson gson = new GsonBuilder()  
  32.                 .setVersion(1)//版本为1  
  33.                 .create();  
  34.         User user1 = gson.fromJson(jsonFromServer, User.class);  
  35.         log("------>反序列化v=1:" + user1);  
  36.   
  37.         User user1_1 = new User();  
  38.         user1_1.name = "zhangsan2";  
  39.         String userStr = gson.toJson(user1_1);  
  40.         log("------>序列化v=1:" + userStr);  
  41.   
  42.   
  43.         Gson gson2 = new GsonBuilder()  
  44.                 .setVersion(2)//版本为2  
  45.                 .create();  
  46.         User user2 = gson2.fromJson(jsonFromServer, User.class);  
  47.         log("------>反序列化v=2:" + user2);  
  48.   
  49.         User user2_1 = new User();  
  50.         user2_1.name = "zhangsan2";  
  51.         String userStr2_1 = gson2.toJson(user2_1);  
  52.         log("------>序列化v=2:" + userStr2_1);  
  53.   
  54.         Gson gson3 = new GsonBuilder()  
  55.                 .setVersion(3)//版本为3  
  56.                 .create();  
  57.         User user3 = gson3.fromJson(jsonFromServer, User.class);  
  58.         log("------>反序列化v=3:" + user3);  
  59.   
  60.         User user3_1 = new User();  
  61.         user3_1.name = "zhangsan2";  
  62.         String userStr3_1 = gson3.toJson(user3_1);  
  63.         log("------>序列化v=3:" + userStr3_1);  
  64.     }  
  65. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Since;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest6 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        @Since(2)
        public String name;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        String jsonFromServer = "{\"name\": \"zhangsan\"}";
        Gson gson = new GsonBuilder()
                .setVersion(1)//版本为1
                .create();
        User user1 = gson.fromJson(jsonFromServer, User.class);
        log("------>反序列化v=1:" + user1);

        User user1_1 = new User();
        user1_1.name = "zhangsan2";
        String userStr = gson.toJson(user1_1);
        log("------>序列化v=1:" + userStr);


        Gson gson2 = new GsonBuilder()
                .setVersion(2)//版本为2
                .create();
        User user2 = gson2.fromJson(jsonFromServer, User.class);
        log("------>反序列化v=2:" + user2);

        User user2_1 = new User();
        user2_1.name = "zhangsan2";
        String userStr2_1 = gson2.toJson(user2_1);
        log("------>序列化v=2:" + userStr2_1);

        Gson gson3 = new GsonBuilder()
                .setVersion(3)//版本为3
                .create();
        User user3 = gson3.fromJson(jsonFromServer, User.class);
        log("------>反序列化v=3:" + user3);

        User user3_1 = new User();
        user3_1.name = "zhangsan2";
        String userStr3_1 = gson3.toJson(user3_1);
        log("------>序列化v=3:" + userStr3_1);
    }
}

运行结果:

------>反序列化v=1:User{name='null'}
------>序列化v=1:{}
------>反序列化v=2:User{name='zhangsan'}
------>序列化v=2:{"name":"zhangsan2"}
------>反序列化v=3:User{name='zhangsan'}
------>序列化v=3:{"name":"zhangsan2"}



    @Util(float v)注解 版本控制

      当gson的setVersion(n) n<v 才解析

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.GsonBuilder;  
  5. import com.google.gson.annotations.Until;  
  6.   
  7. /** 
  8.  * @author xuanyouwu 
  9.  * @email xuanyouwu@163.com 
  10.  * @time 2016-05-18 11:20 
  11.  */  
  12. public class GsonTest6 {  
  13.     private static void log(String msg) {  
  14.         System.out.println(msg);  
  15.     }  
  16.   
  17.     public static class User {  
  18.         @Until(2)  
  19.         public String name;  
  20.   
  21.         @Override  
  22.         public String toString() {  
  23.             return "User{" +  
  24.                     "name='" + name + '\'' +  
  25.                     '}';  
  26.         }  
  27.     }  
  28.   
  29.     public static void main(String[] args) throws Exception {  
  30.         String jsonFromServer = "{\"name\": \"zhangsan\"}";  
  31.         Gson gson = new GsonBuilder()  
  32.                 .setVersion(1)//版本为1  
  33.                 .create();  
  34.         User user1 = gson.fromJson(jsonFromServer, User.class);  
  35.         log("------>反序列化v=1:" + user1);  
  36.   
  37.         User user1_1 = new User();  
  38.         user1_1.name = "zhangsan2";  
  39.         String userStr = gson.toJson(user1_1);  
  40.         log("------>序列化v=1:" + userStr);  
  41.   
  42.   
  43.         Gson gson2 = new GsonBuilder()  
  44.                 .setVersion(2)//版本为2  
  45.                 .create();  
  46.         User user2 = gson2.fromJson(jsonFromServer, User.class);  
  47.         log("------>反序列化v=2:" + user2);  
  48.   
  49.         User user2_1 = new User();  
  50.         user2_1.name = "zhangsan2";  
  51.         String userStr2_1 = gson2.toJson(user2_1);  
  52.         log("------>序列化v=2:" + userStr2_1);  
  53.   
  54.         Gson gson3 = new GsonBuilder()  
  55.                 .setVersion(3)//版本为3  
  56.                 .create();  
  57.         User user3 = gson3.fromJson(jsonFromServer, User.class);  
  58.         log("------>反序列化v=3:" + user3);  
  59.   
  60.         User user3_1 = new User();  
  61.         user3_1.name = "zhangsan2";  
  62.         String userStr3_1 = gson3.toJson(user3_1);  
  63.         log("------>序列化v=3:" + userStr3_1);  
  64.     }  
  65. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Until;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest6 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        @Until(2)
        public String name;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        String jsonFromServer = "{\"name\": \"zhangsan\"}";
        Gson gson = new GsonBuilder()
                .setVersion(1)//版本为1
                .create();
        User user1 = gson.fromJson(jsonFromServer, User.class);
        log("------>反序列化v=1:" + user1);

        User user1_1 = new User();
        user1_1.name = "zhangsan2";
        String userStr = gson.toJson(user1_1);
        log("------>序列化v=1:" + userStr);


        Gson gson2 = new GsonBuilder()
                .setVersion(2)//版本为2
                .create();
        User user2 = gson2.fromJson(jsonFromServer, User.class);
        log("------>反序列化v=2:" + user2);

        User user2_1 = new User();
        user2_1.name = "zhangsan2";
        String userStr2_1 = gson2.toJson(user2_1);
        log("------>序列化v=2:" + userStr2_1);

        Gson gson3 = new GsonBuilder()
                .setVersion(3)//版本为3
                .create();
        User user3 = gson3.fromJson(jsonFromServer, User.class);
        log("------>反序列化v=3:" + user3);

        User user3_1 = new User();
        user3_1.name = "zhangsan2";
        String userStr3_1 = gson3.toJson(user3_1);
        log("------>序列化v=3:" + userStr3_1);
    }
}

运行结果:

------>反序列化v=1:User{name='zhangsan'}
------>序列化v=1:{"name":"zhangsan2"}
------>反序列化v=2:User{name='null'}
------>序列化v=2:{}
------>反序列化v=3:User{name='null'}
------>序列化v=3:{}


Gson 高级用法

    相信看过retrofit2.0 源码的同学都知道,其中有一个GsonConverterFactory 里面的用法十分精炼老成,这里来一段源码

 



   据说使用TypeAdapter 效率更高,本人还未对比测试,暂时放后吧,TypeAdapter是什么玩意呢?

   在源码中备注了一句 Converts Java objects to and from JSON 就是对象json之间的互相转换 接替了T 泛型类的序列化和反序列化的逻辑

   从源码中我们看到区分了2.1版本之前后之后的用法,2.1版本之前可以自定义adapter:

   

  1. public class PointAdapter extends TypeAdapter<Point> {  
  2.  *     public Point read(JsonReader reader) throws IOException {  
  3.  *       if (reader.peek() == JsonToken.NULL) {  
  4.  *         reader.nextNull();  
  5.  *         return null;  
  6.  *       }  
  7.  *       String xy = reader.nextString();  
  8.  *       String[] parts = xy.split(",");  
  9.  *       int x = Integer.parseInt(parts[0]);  
  10.  *       int y = Integer.parseInt(parts[1]);  
  11.  *       return new Point(x, y);  
  12.  *     }  
  13.  *     public void write(JsonWriter writer, Point value) throws IOException {  
  14.  *       if (value == null) {  
  15.  *         writer.nullValue();  
  16.  *         return;  
  17.  *       }  
  18.  *       String xy = value.getX() + "," + value.getY();  
  19.  *       writer.value(xy);  
  20.  *     }  
  21.  *   }}  
public class PointAdapter extends TypeAdapter<Point> {
 *     public Point read(JsonReader reader) throws IOException {
 *       if (reader.peek() == JsonToken.NULL) {
 *         reader.nextNull();
 *         return null;
 *       }
 *       String xy = reader.nextString();
 *       String[] parts = xy.split(",");
 *       int x = Integer.parseInt(parts[0]);
 *       int y = Integer.parseInt(parts[1]);
 *       return new Point(x, y);
 *     }
 *     public void write(JsonWriter writer, Point value) throws IOException {
 *       if (value == null) {
 *         writer.nullValue();
 *         return;
 *       }
 *       String xy = value.getX() + "," + value.getY();
 *       writer.value(xy);
 *     }
 *   }}

    使用

  

  1. GsonBuilder builder = new GsonBuilder();  
  2.  *   builder.registerTypeAdapter(Point.classnew PointAdapter());  
  3.  *   // if PointAdapter didn't check for nulls in its read/write methods, you should instead use  
  4.  *   // builder.registerTypeAdapter(Point.class, new PointAdapter().nullSafe());  
  5.  *   ...  
  6.  *   Gson gson = builder.create();  
GsonBuilder builder = new GsonBuilder();
 *   builder.registerTypeAdapter(Point.class, new PointAdapter());
 *   // if PointAdapter didn't check for nulls in its read/write methods, you should instead use
 *   // builder.registerTypeAdapter(Point.class, new PointAdapter().nullSafe());
 *   ...
 *   Gson gson = builder.create();

    在2.1版本之后更推荐直接插入泛型就使用

  
  
  1. //   String json = "{'origin':'0,0','points':['1,2','3,4']}";  
  2. //   TypeAdapter<Graph> graphAdapter = gson.getAdapter(Graph.class);  
  3. //   Graph graph = graphAdapter.fromJson(json);  
  4. // }</pre>  
  5. // And an example for serialization: <pre>   {@code  
  6. //  
  7. //   Graph graph = new Graph(...);  
  8. //   TypeAdapter<Graph> graphAdapter = gson.getAdapter(Graph.class);  
  9. //   String json = graphAdapter.toJson(graph);  
  10. // }</pre>  
//   String json = "{'origin':'0,0','points':['1,2','3,4']}";
//   TypeAdapter<Graph> graphAdapter = gson.getAdapter(Graph.class);
//   Graph graph = graphAdapter.fromJson(json);
// }</pre>
// And an example for serialization: <pre>   {@code
//
//   Graph graph = new Graph(...);
//   TypeAdapter<Graph> graphAdapter = gson.getAdapter(Graph.class);
//   String json = graphAdapter.toJson(graph);
// }</pre>

   实例:使用TypeAdapter 来序列化和反序列化
   
  
  
  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.TypeAdapter;  
  5.   
  6. /** 
  7.  * @author xuanyouwu 
  8.  * @email xuanyouwu@163.com 
  9.  * @time 2016-05-18 11:20 
  10.  */  
  11. public class GsonTest7 {  
  12.     private static void log(String msg) {  
  13.         System.out.println(msg);  
  14.     }  
  15.   
  16.     public static class User {  
  17.         public String name;  
  18.         public int age;  
  19.   
  20.         @Override  
  21.         public String toString() {  
  22.             return "User{" +  
  23.                     "name='" + name + '\'' +  
  24.                     ", age=" + age +  
  25.                     '}';  
  26.         }  
  27.     }  
  28.   
  29.     public static void main(String[] args) throws Exception {  
  30.         Gson gson = new Gson();  
  31.         TypeAdapter<User> userTypeAdapter = gson.getAdapter(User.class);  
  32.         User user = new User();  
  33.         user.name = "xuanyouwu";  
  34.         user.age = 26;  
  35.         String userJsonStr = userTypeAdapter.toJson(user);  
  36.         log("------>序列化:" + userJsonStr);  
  37.   
  38.         User user1 = userTypeAdapter.fromJson(userJsonStr);  
  39.         log("------>反序列化:" + user1);  
  40.   
  41.     }  
  42. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest7 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        public String name;
        public int age;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        Gson gson = new Gson();
        TypeAdapter<User> userTypeAdapter = gson.getAdapter(User.class);
        User user = new User();
        user.name = "xuanyouwu";
        user.age = 26;
        String userJsonStr = userTypeAdapter.toJson(user);
        log("------>序列化:" + userJsonStr);

        User user1 = userTypeAdapter.fromJson(userJsonStr);
        log("------>反序列化:" + user1);

    }
}
  1. 运行结果:  
运行结果:
  1. ------>序列化:{"name":"xuanyouwu","age":26}  
  2. ------>反序列化:User{name='xuanyouwu', age=26}  
------>序列化:{"name":"xuanyouwu","age":26}
------>反序列化:User{name='xuanyouwu', age=26}

Gson的容错机制:
   为什么要容错了,在javaBean中声明了int类型的age 如果服务端 返回的是"" 空字符串怎么办呢?崩溃?
   如果json格式不规范如 {name=zhangsan,age:26,hobby=null}  发现不是普通的key value
  容错实现方式1:
  1:创建Gson的方式
  
  
  1. gson = new GsonBuilder()  
  2.                   .setLenient()// json宽松  
  3.                   .create();  
  gson = new GsonBuilder()
                    .setLenient()// json宽松
                    .create();
2:使用JsonReader
   JsonReader jsonReader = gson.newJsonReader(value.charStream());
  
  
  1. jsonReader.<span style="font-family: 宋体; font-size: 9pt;">setLenient(true)</span>  
   jsonReader.<span style="font-family: 宋体; font-size: 9pt;">setLenient(true)</span>

    3:自定义TypeAdapter

    4:使用注解JsonAdapter,其实也是自定义Adapter

 

 1.2归为一类 由框架实现,基本json大格式规范,键值对不标准,多引号的问题等等,而不报错停止解析,但是功能相对较弱

 能解决bug 

  1. com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON<span style="font-family:宋体; font-size:9pt; background-color:rgb(228,228,255)"> </span>  
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON 

3,4归为一类,都属于自定义adapter,但是3与gson绑定,4使用注解和字段绑定

 实例:

   

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.GsonBuilder;  
  5. import com.google.gson.JsonParseException;  
  6. import com.google.gson.TypeAdapter;  
  7. import com.google.gson.stream.JsonReader;  
  8. import com.google.gson.stream.JsonWriter;  
  9.   
  10. import java.io.IOException;  
  11.   
  12. /** 
  13.  * @author xuanyouwu 
  14.  * @email xuanyouwu@163.com 
  15.  * @time 2016-05-18 11:20 
  16.  */  
  17. public class GsonTest8 {  
  18.     private static void log(String msg) {  
  19.         System.out.println(msg);  
  20.     }  
  21.   
  22.     public static class User {  
  23.         public String name;  
  24.         public int age;  
  25.   
  26.         @Override  
  27.         public String toString() {  
  28.             return "User{" +  
  29.                     "name='" + name + '\'' +  
  30.                     ", age=" + age +  
  31.                     '}';  
  32.         }  
  33.   
  34.   
  35.         ;  
  36.     }  
  37.   
  38.     public static class UserTypeAdapter extends TypeAdapter<User> {  
  39.   
  40.         @Override  
  41.         public void write(JsonWriter out, User value) throws IOException {  
  42.             out.beginObject();  
  43.             out.name("name").value(value.name);  
  44.             out.name("age").value(value.age);  
  45.             out.endObject();  
  46.         }  
  47.   
  48.         @Override  
  49.         public User read(JsonReader in) throws IOException {  
  50.             User user = new User();  
  51.             in.beginObject();  
  52.             while (in.hasNext()) {  
  53.                 switch (in.nextName()) {  
  54.                     case "name":  
  55.                         user.name = in.nextString();  
  56.                         break;  
  57.                     case "age":  
  58.                         try {  
  59.                             String str = in.nextString();  
  60.                             user.age = Integer.valueOf(str);  
  61.                         } catch (Exception e) {  
  62.                         }  
  63.                         break;  
  64.                 }  
  65.             }  
  66.             in.endObject();  
  67.             return user;  
  68.         }  
  69.     }  
  70.   
  71.     public static void main(String[] args) throws Exception {  
  72.         Gson gson = new Gson();  
  73.   
  74.         String jsonStrFromServer = "{\n" +  
  75.                 "    \"age\": \"\",\n" +  
  76.                 "    \"name\": \"zhangsan\"\n" +  
  77.                 "}";  
  78.         log("------->jsonFromServer:"+jsonStrFromServer);  
  79.         try {  
  80.             User user = gson.fromJson(jsonStrFromServer, User.class);  
  81.             log("------>默认Gson 解析:" + user);  
  82.         } catch (JsonParseException e) {//java.lang.NumberFormatException: empty String  
  83.             log("------>默认Gson 解析 异常:" + e);  
  84.         }  
  85.   
  86.         Gson gson2 = new GsonBuilder()  
  87.                 .registerTypeAdapter(User.classnew UserTypeAdapter()).create();  
  88.         try {  
  89.             User user2 = gson2.fromJson(jsonStrFromServer, User.class);  
  90.             log("------>自定义adapter 解析:" + user2);  
  91.         } catch (JsonParseException e) {//java.lang.NumberFormatException: empty String  
  92.             log("------>自定义adapter 异常:" + e);  
  93.         }  
  94.   
  95.         try {  
  96.             UserTypeAdapter userTypeAdapter = new UserTypeAdapter();  
  97.             User user3 = userTypeAdapter.fromJson(jsonStrFromServer);  
  98.             log("------>自定义adapter 解析2:" + user3);  
  99.         } catch (Exception e) {  
  100.             log("------>自定义adapter 异常2:" + e);  
  101.         }  
  102.     }  
  103. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest8 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        public String name;
        public int age;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }


        ;
    }

    public static class UserTypeAdapter extends TypeAdapter<User> {

        @Override
        public void write(JsonWriter out, User value) throws IOException {
            out.beginObject();
            out.name("name").value(value.name);
            out.name("age").value(value.age);
            out.endObject();
        }

        @Override
        public User read(JsonReader in) throws IOException {
            User user = new User();
            in.beginObject();
            while (in.hasNext()) {
                switch (in.nextName()) {
                    case "name":
                        user.name = in.nextString();
                        break;
                    case "age":
                        try {
                            String str = in.nextString();
                            user.age = Integer.valueOf(str);
                        } catch (Exception e) {
                        }
                        break;
                }
            }
            in.endObject();
            return user;
        }
    }

    public static void main(String[] args) throws Exception {
        Gson gson = new Gson();

        String jsonStrFromServer = "{\n" +
                "    \"age\": \"\",\n" +
                "    \"name\": \"zhangsan\"\n" +
                "}";
        log("------->jsonFromServer:"+jsonStrFromServer);
        try {
            User user = gson.fromJson(jsonStrFromServer, User.class);
            log("------>默认Gson 解析:" + user);
        } catch (JsonParseException e) {//java.lang.NumberFormatException: empty String
            log("------>默认Gson 解析 异常:" + e);
        }

        Gson gson2 = new GsonBuilder()
                .registerTypeAdapter(User.class, new UserTypeAdapter()).create();
        try {
            User user2 = gson2.fromJson(jsonStrFromServer, User.class);
            log("------>自定义adapter 解析:" + user2);
        } catch (JsonParseException e) {//java.lang.NumberFormatException: empty String
            log("------>自定义adapter 异常:" + e);
        }

        try {
            UserTypeAdapter userTypeAdapter = new UserTypeAdapter();
            User user3 = userTypeAdapter.fromJson(jsonStrFromServer);
            log("------>自定义adapter 解析2:" + user3);
        } catch (Exception e) {
            log("------>自定义adapter 异常2:" + e);
        }
    }
}

运行结果:

------->jsonFromServer:{
    "age": "",
    "name": "zhangsan"
}
------>默认Gson 解析 异常:com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String
------>自定义adapter 解析:User{name='zhangsan', age=0}
------>自定义adapter 解析2:User{name='zhangsan', age=0}

可以看到 age是空字符串 但是不影响整体的解析流程,这对客户端是十分友好的


  基于注解的方式,上面的方式倾向于整体,注解的方式倾向于字段

  

  1. package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.JsonParseException;  
  5. import com.google.gson.TypeAdapter;  
  6. import com.google.gson.annotations.JsonAdapter;  
  7. import com.google.gson.stream.JsonReader;  
  8. import com.google.gson.stream.JsonWriter;  
  9.   
  10. import java.io.IOException;  
  11.   
  12. /** 
  13.  * @author xuanyouwu 
  14.  * @email xuanyouwu@163.com 
  15.  * @time 2016-05-18 11:20 
  16.  */  
  17. public class GsonTest9 {  
  18.     private static void log(String msg) {  
  19.         System.out.println(msg);  
  20.     }  
  21.   
  22.     public static class User {  
  23.         public String name;  
  24.         @JsonAdapter(IntegerTypeAdapter.class)  
  25.         public int age;  
  26.   
  27.         @Override  
  28.         public String toString() {  
  29.             return "User{" +  
  30.                     "name='" + name + '\'' +  
  31.                     ", age=" + age +  
  32.                     '}';  
  33.         }  
  34.     }  
  35.   
  36.     public static class IntegerTypeAdapter extends TypeAdapter<Integer> {  
  37.   
  38.         @Override  
  39.         public void write(JsonWriter out, Integer value) throws IOException {  
  40.             out.value(value);  
  41.         }  
  42.   
  43.         @Override  
  44.         public Integer read(JsonReader in) throws IOException {  
  45.             int i = 0;  
  46.             try {  
  47.                 String str = in.nextString();  
  48.                 i = Integer.valueOf(str);  
  49.             } catch (Exception e) {  
  50.             }  
  51.             return i;  
  52.         }  
  53.     }  
  54.   
  55.     public static class User2 {  
  56.         public String name;  
  57.         public int age;  
  58.   
  59.         @Override  
  60.         public String toString() {  
  61.             return "User{" +  
  62.                     "name='" + name + '\'' +  
  63.                     ", age=" + age +  
  64.                     '}';  
  65.         }  
  66.     }  
  67.   
  68.   
  69.     public static void main(String[] args) throws Exception {  
  70.         Gson gson = new Gson();  
  71.   
  72.         String jsonStrFromServer = "{\n" +  
  73.                 "    \"age\": \"\",\n" +  
  74.                 "    \"name\": \"zhangsan\"\n" +  
  75.                 "}";  
  76.         log("------->jsonFromServer:" + jsonStrFromServer);  
  77.         try {  
  78.             User2 user2 = gson.fromJson(jsonStrFromServer, User2.class);  
  79.             log("------>gson 解析:" + user2);  
  80.         } catch (Exception e) {  
  81.             log("------>gson 解析异常:" + e);  
  82.         }  
  83.   
  84.   
  85.         try {  
  86.             User user = gson.fromJson(jsonStrFromServer, User.class);  
  87.             log("------>JsonAdapter 注解 解析:" + user);  
  88.         } catch (JsonParseException e) {//java.lang.NumberFormatException: empty String  
  89.             log("------>JsonAdapter 注解 异常:" + e);  
  90.         }  
  91.     }  
  92. }  
package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest9 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        public String name;
        @JsonAdapter(IntegerTypeAdapter.class)
        public int age;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    public static class IntegerTypeAdapter extends TypeAdapter<Integer> {

        @Override
        public void write(JsonWriter out, Integer value) throws IOException {
            out.value(value);
        }

        @Override
        public Integer read(JsonReader in) throws IOException {
            int i = 0;
            try {
                String str = in.nextString();
                i = Integer.valueOf(str);
            } catch (Exception e) {
            }
            return i;
        }
    }

    public static class User2 {
        public String name;
        public int age;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }


    public static void main(String[] args) throws Exception {
        Gson gson = new Gson();

        String jsonStrFromServer = "{\n" +
                "    \"age\": \"\",\n" +
                "    \"name\": \"zhangsan\"\n" +
                "}";
        log("------->jsonFromServer:" + jsonStrFromServer);
        try {
            User2 user2 = gson.fromJson(jsonStrFromServer, User2.class);
            log("------>gson 解析:" + user2);
        } catch (Exception e) {
            log("------>gson 解析异常:" + e);
        }


        try {
            User user = gson.fromJson(jsonStrFromServer, User.class);
            log("------>JsonAdapter 注解 解析:" + user);
        } catch (JsonParseException e) {//java.lang.NumberFormatException: empty String
            log("------>JsonAdapter 注解 异常:" + e);
        }
    }
}

运行结果:

  1. ------->jsonFromServer:{  
  2.     "age""",  
  3.     "name""zhangsan"  
  4. }  
  5. ------>gson 解析异常:com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String  
  6. ------>JsonAdapter 注解 解析:User{name='zhangsan', age=0}  
------->jsonFromServer:{
    "age": "",
    "name": "zhangsan"
}
------>gson 解析异常:com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String
------>JsonAdapter 注解 解析:User{name='zhangsan', age=0}
  1. 可以看到我们成功地跳过服务端返回json不合理的坑了吧  
可以看到我们成功地跳过服务端返回json不合理的坑了吧
  1. </pre><pre code_snippet_id="1687762" snippet_file_name="blog_20160519_47_1535429" name="code" class="java">不得不吐槽啊,像这种服务器端错误,应该完全归结服务端json不合理返回,  
</pre><pre code_snippet_id="1687762" snippet_file_name="blog_20160519_47_1535429" name="code" class="java">不得不吐槽啊,像这种服务器端错误,应该完全归结服务端json不合理返回,
  1. 往往老板都会找客户端原因,怎么又崩溃了?怎么又不显示了?老板不会关心是不是数据返回不合理的问题的!  
往往老板都会找客户端原因,怎么又崩溃了?怎么又不显示了?老板不会关心是不是数据返回不合理的问题的!
  1. 做客户端需要相当的承受能力哈,同意的点个赞哈  
做客户端需要相当的承受能力哈,同意的点个赞哈
  1. </pre><p>其实上面的方式保险是很保险,但是需要维护的地方多,代码量大,我们介绍另外一种方式:JsonSerializer与JsonDeserializer 之关系单方面的处理,可以全局注册某个类型的处理:看实例:</p><p><pre name="code" class="java">package com.xuan.gson;  
  2.   
  3. import com.google.gson.Gson;  
  4. import com.google.gson.GsonBuilder;  
  5. import com.google.gson.JsonDeserializationContext;  
  6. import com.google.gson.JsonDeserializer;  
  7. import com.google.gson.JsonElement;  
  8. import com.google.gson.JsonParseException;  
  9.   
  10. import java.lang.reflect.Type;  
  11.   
  12. /** 
  13.  * @author xuanyouwu 
  14.  * @email xuanyouwu@163.com 
  15.  * @time 2016-05-18 11:20 
  16.  */  
  17. public class GsonTest10 {  
  18.     private static void log(String msg) {  
  19.         System.out.println(msg);  
  20.     }  
  21.   
  22.     public static class User {  
  23.         public String name;  
  24.         public int age;  
  25.   
  26.         @Override  
  27.         public String toString() {  
  28.             return "User{" +  
  29.                     "name='" + name + '\'' +  
  30.                     ", age=" + age +  
  31.                     '}';  
  32.         }  
  33.     }  
  34.   
  35.   
  36.     public static void main(String[] args) throws Exception {  
  37.         JsonDeserializer<Integer> jsonDeserializer = new JsonDeserializer<Integer>() {  
  38.             @Override  
  39.             public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {  
  40.                 try {  
  41.                     return json.getAsInt();  
  42.                 } catch (NumberFormatException e) {  
  43.                     return 0;  
  44.                 }  
  45.             }  
  46.         };  
  47.         Gson gson = new GsonBuilder()  
  48.                 .registerTypeAdapter(int.class, jsonDeserializer)  
  49.                 .create();  
  50.   
  51.         String jsonStrFromServer = "{\n" +  
  52.                 "    \"age\": \"\",\n" +  
  53.                 "    \"name\": \"zhangsan\"\n" +  
  54.                 "}";  
  55.         log("------->jsonFromServer:" + jsonStrFromServer);  
  56.         try {  
  57.             User user = gson.fromJson(jsonStrFromServer, User.class);  
  58.             log("------>  JsonDeserializer<Integer> 解析:" + user);  
  59.         } catch (Exception e) {  
  60.             log("------>  JsonDeserializer<Integer> 解析异常:" + e);  
  61.         }  
  62.   
  63.         Gson gson1=new Gson();  
  64.         try {  
  65.             User user1 = gson1.fromJson(jsonStrFromServer, User.class);  
  66.             log("------> 默认gson 解析:" + user1);  
  67.         } catch (Exception e) {  
  68.             log("------>  默认gson 解析异常:" + e);  
  69.         }  
  70.   
  71.   
  72.     }  
  73. }  
</pre><p>其实上面的方式保险是很保险,但是需要维护的地方多,代码量大,我们介绍另外一种方式:JsonSerializer与JsonDeserializer 之关系单方面的处理,可以全局注册某个类型的处理:看实例:</p><p><pre name="code" class="java">package com.xuan.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;

/**
 * @author xuanyouwu
 * @email xuanyouwu@163.com
 * @time 2016-05-18 11:20
 */
public class GsonTest10 {
    private static void log(String msg) {
        System.out.println(msg);
    }

    public static class User {
        public String name;
        public int age;

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }


    public static void main(String[] args) throws Exception {
        JsonDeserializer<Integer> jsonDeserializer = new JsonDeserializer<Integer>() {
            @Override
            public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                try {
                    return json.getAsInt();
                } catch (NumberFormatException e) {
                    return 0;
                }
            }
        };
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(int.class, jsonDeserializer)
                .create();

        String jsonStrFromServer = "{\n" +
                "    \"age\": \"\",\n" +
                "    \"name\": \"zhangsan\"\n" +
                "}";
        log("------->jsonFromServer:" + jsonStrFromServer);
        try {
            User user = gson.fromJson(jsonStrFromServer, User.class);
            log("------>  JsonDeserializer<Integer> 解析:" + user);
        } catch (Exception e) {
            log("------>  JsonDeserializer<Integer> 解析异常:" + e);
        }

        Gson gson1=new Gson();
        try {
            User user1 = gson1.fromJson(jsonStrFromServer, User.class);
            log("------> 默认gson 解析:" + user1);
        } catch (Exception e) {
            log("------>  默认gson 解析异常:" + e);
        }


    }
}
运行结果:

------->jsonFromServer:{
    "age": "",
    "name": "zhangsan"
}
------>  JsonDeserializer<Integer> 解析:User{name='zhangsan', age=0}
------>  默认gson 解析异常:com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String

 这样定义全局的反序列化工具就能避免解析异常


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值