标签:
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 可以配置多种选项
gson = new GsonBuilder() .setLenient()// json宽松 .enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式 .serializeNulls() //智能null .setPrettyPrinting()// 调教格式 .disableHtmlEscaping() //默认是GSON把HTML 转义的 .create();Gson的基本用法:
JavaBean转换Json字符串
gson提供 publicString toJson(Object src) 方法可以将对象转换成jsonStr
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字符串
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)); } }
运行结果:
---->list convert jsonStr:["1","a","3","rt","5"] ---->map convert jsonStr:{"name":"xuanyouwu","age":"26"}
Json字符串转JavaBean
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
String listJsonStr="[\"1\",\"a\",\"3\",\"rt\",\"5\"]";
Type type = new TypeToken<ArrayList<String>>() { }.getType(); ArrayList<String> sList=gson.fromJson(listJsonStr, type); log("------->json convert List:"+sList);
运行结果:
------->json convert List:[1, a, 3, rt, 5]
</pre><pre code_snippet_id="1687762" snippet_file_name="blog_20160518_11_939636" name="code" class="java"> Gson封装的类型体系
Gson里面有一个非常有意思的抽象基类JsonElement,他的继承体系:
<img src="http://img.blog.csdn.net/20160518135136864?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
JsonObject等同于org的JSONObject,JsonArray也类似,
JsonNull 其实就是null 字段
JsonNull jsonNull=new JsonNull();//构造方法过时,推荐INSTANCE
JsonNull jsonNull=JsonNull.INSTANCE; log("-----"+jsonNull);
运行结果:
-----null
JsonPrimitive非常有意思,我们知道如果json转换成字符串 可能包含引号的转义,但是通过JsonPrimative我们可以获得为转义的字符串,看实例:
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类似
JsonObject jsonObject=new JsonObject(); jsonObject.addProperty("name","xuanyouwu"); jsonObject.addProperty("age",26); log("------>create jsonObject:"+jsonObject);运行结果:
------>create jsonObject:{"name":"xuanyouwu","age":26}
创建JsonArray
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 添加一个数组的字段
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 比较优雅简介
实例:
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}
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的备选字段 会后面的替换前面的
实例:
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过滤注解
源码:默认既可以序列化又可以反序列化
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Expose { public boolean serialize() default true; public boolean deserialize() default true; }
</pre><pre code_snippet_id="1687762" snippet_file_name="blog_20160518_25_4965554" name="code" class="java">可以排除不需要序列化的字段,需要配合GsonBuilder使用
<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)
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注解
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注解 只序列化
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 只反序列化
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时 才会序列化解析
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 才解析
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:
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); * } * }}
使用
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版本之后更推荐直接插入泛型就使用
// 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 来序列化和反序列化
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); } }运行结果:------>序列化:{"name":"xuanyouwu","age":26} ------>反序列化:User{name='xuanyouwu', age=26}
Gson的容错机制:
为什么要容错了,在javaBean中声明了int类型的age 如果服务端 返回的是"" 空字符串怎么办呢?崩溃?
容错实现方式1:
1:创建Gson的方式gson = new GsonBuilder() .setLenient()// json宽松 .create();2:使用JsonReader
JsonReader jsonReader = gson.newJsonReader(value.charStream());jsonReader.<span style="font-family: 宋体; font-size: 9pt;">setLenient(true)</span>
3:自定义TypeAdapter
4:使用注解JsonAdapter,其实也是自定义Adapter
1.2归为一类 由框架实现,基本json大格式规范,数据类型不匹配会自动赋予默认值,而不报错停止解析
实例:
3,4归为一类,都属于自定义adapter,但是3与gson绑定,4使用注解和字段绑定
实例:
标签:
原文地址:http://blog.csdn.net/axuanqq/article/details/51441590