【Gson】【2】Gson使用演示

简介:
原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://qingkechina.blog.51cto.com/5552198/1290557

3、【原始类型演示】

(1)原始类型转换为JSON对象(序列化

Gson gson = new Gson();


gson.toJson(1); // 转换后的JSON为1

gson.toJson("abcd"); // 转换后的JSON为"abcd"

gson.toJson(new Long(10)); //转换后的JSON为10

gson.toJson(new int[]{1, 2}); //转换后的JSON为[1, 2]


(2)JSON对象转换为原始类型(列化

Gson gson = new Gson();


int one = gson.fromJson("1", int.class); // 转换后的Java原始类型值为1

Integer ten = gson.fromJson("10", Integer.class); // 转换后的Java原始类型值为10

Long five = gson.fromJson("5", Long.class); //转换后的Java原始类型值为5

Boolean b = gson.fromJson("false", Boolean.class); //转换后的Java原始类型值为false

String str = gson.fromJson("\"abc\"", String.class); //转换后的Java原始类型值为"abc"

String anotherStr = gons.fromJson("[\"abcdef\"]", String.class); //转换后的Java原始类型值为"abcdef"


4、【Java对象演示】

(1)Java对象转换为JSON对象(序列化

public class BagOfPrimitives

{

    private int value1 = 1;

    private String value2 = "abc";

    private transient int value3 = 3;


BagOfPrimitives()

    {

    }


    public static void main(String[] args)

    {

         Gson gson = new Gson();

BagOfPrimitives obj = new BagOfPrimitives();

         String json = gson.toJson(obj);


         System.out.println(json); // 转换后的JSON为{"value1": 1, "value2":"abc"}

    }

}



i_f42.gif细心的读者可能已发现transient修饰的字段没有转换为JSON



(2)JSON对象转换为Java对象(列化

String json = "{\"value1\": 1, \"value2\": \"abc\", \"value3\": 8}";

BagOfPrimitives obj = gson.fromJson(json, BagOfPrimitives.class);



i_f42.gifJSON结构中的value3尽管被赋值为8,但转换为BagOfPrimitives对象后value3的属性值依然为定义时的值。



5、【GSON可以把JSON转换为静态内部类,但不能转换所谓纯内部类】

例如:

public class A

{

   public String a = null;


   class B

   {

        public String b = null;

   }


   public static void main(String[] args)

   {

       Gson gson = new Gson();

       String json = "{\"b\": \"def\"}";

       A obj = gson.fromJson(json, A.class);

   }

}

在转换过程中会抛出“No-args constructor for class com.pwm.gson.A$B does not exist”异常。解析方法把内部类改为静态内部类,或者按异常信息“Register an InstanceCreator with Gson for this type to fix this problem”所示,用Gson为其提供一个内部实例创建器,但GSON强烈不建议使用此方法。


6、【数组演示】:

(1)数据转换为JSON对象(序列化

Gson gson = new Gson();

int[] ints = {1, 2, 3, 4, 5};

String[] strings = {"abc", "def", "ghi"};


gson.toJson(ints); // 转换后的JSON为[1,2,3,4,5]

gson.toJson(strings); // 转换后的JSON为["abc","def","ghi"]


(2)JSON对象转换为数组(列化

Gson gson = new Gson();

String arrays = "[1,2,3,4,5]";

int[] ints2 = gson.fromJson(arrays, int[].class); // 转换后的Java整型数组

String[] strs2 = gson.fromJson(arrays, String[].class);  //转换后的Java字符串数组


7、【集合演示】

(1)集合转换为JSON对象(序列化

Gson gson = new Gson();

Collection<Integer> ints = new ArrayList<Integer>();

ints.add(new Integer(1));

ints.add(new Integer(11));

ints.add(new Integer(111));


gson.toJson(ints); // 转换后的JSON为[1,11,111]


(2)JSON对象转换为集合(列化

Gson gson = new Gson();

String colJson = "[2, 22, 222]";

Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();


Collection<Integer> ints2 = gson.fromJson(colJson, collectionType); // 转换后的Java整数集合



i_f42.gifGSON官方资料中关于集合有个限制声明:

  • 对于任意类型的对象总能序列化为JSON对象,但不能把它反序列化过来,因为对用户来讲没有通常办法决定返回值的类型

  • 当反序列化为JAVA对象时,必须为集合指定类型



8、【泛型演示】

当使用toJson(obj)时,GSON会调用obj.getClass()获取对象类型信息再进行序列化,类似地当调用fromJson(json, MyClass.class)时,GSON会根据类型信息很好地完成反序列化(把JSON转换为JAVA对象),但当对象是一个泛型时则玩不转了,因为GSON在序列化或反序列化时,JAVA的泛型类型会被擦除掉,从而GSON无法获取泛型对象信息,从而无法完成转换。

(1)举例:

class Foo<T>

{

   T value;

}


Gson gson = new Gson();

Foo<Bar> foo = new Foo<Bar>();


gson.toJson(foo); // 转换JSON对象会失败,它不能正确地序列化foo.value

gson.fromJson(json, foo.getClass()); // 转换JAVA对象会失败,它无法把foo.value作为 Bar反序列化


(2)解决办法

对于这个泛型可以通过TypeTokenrx是定正确的参数类型来解决这个问题,如下:

Type fooType = new TypeToken<Foo<Bar>>(){}.getType();

gson.toJson(foo, fooType);

gson.fromJson(json, fooType);


9、【任意类型集合演示】

有时候我们会处理混合类型的JSON数组,如["hello", 5, {name:"GREETINGS", source:"guest"}],这个数组中既有字符串"hello",又有数值5,同时还有对象{name:"GREETINGS", source:"guest"}

(1)转换为JSON对象序列化

很自然地想到把对象{name:"GREETINGS", source:"guest"}封装成一个JAVA对象

class Event

{

    private String name = null;

    private String source = null;


    Event(String name, String source)

    {

        this.name = name;

        this.source = source;

    }

}


此时可以使用集合进行处理了:

Collection collection = new ArrayList();

collection.add("hello");

collection.add(5);

collection.add(new Event("GREETINGS", "guest"));

对其进行测试,可以发现能成功地转换为JSON对象:

Gson gson = new gson();

gson.toJson(collection); // 转换后的JSON为["hello", 5, {name:"GREETINGS", source:"guest"}]


(2)转换为JAVA对象列化

把上面的JSON对象使用Collection collect = gson.fromJson(json, Collection.class)反序列化,遗憾的是它失败了,抛出“The JsonDeserializer com.google.gson.DefaultTypeAdapter$CollectionTypeAdater@a3bcc1 failed to deserialized json object ["hello", 5, {name:"GREETINGS", source:"guest"}] given the type interface java.util.Collection


(3)解决办法

遇到此种问题通常有三种办法来解决,此处使用最为推荐的方法:使用GSON解析器的API解析数组元素,它可以对每个数组元素使用fromJson()来处理。

JsonParser parser = new JsonParser();

JsonArray array = parser.parse(json).getAsJsonArray();

String ss = gson.fromJson(array.get(0), String.class);

int ii = gson.fromJson(array.get(1), Integer.class);

Event ee = gson.fromJson(array.get(2), Event.class);



10、【空对象演示】

对于空字段GSON缺省行为会忽略掉,但当使用GsonBuilder序列化一个空字段时,Gson将增加一个JsonNull元素到JsonElement结构中,从而可实现空对象的序列化和反序列化,例如:

public class Foo

{

   private final String s;

   private final int i;


   public Foo()

   {

        this(null, 5);

   }


   public Foo(String s, int i)

   {

       this.s = s;

       this.i = i;

   }

}

使用Gson进行测试:

Gson gson = new Gson();

Foo foo = new Foo();

String json = gson.toJson(foo); //转换后的Json为{"i":5},把字符串s忽略掉了

json = gson.toJson(null);       // 转换后的Json为""空字符串


使用GsonBuilder进行测试:

Gson gson = new GsonBuilder().serializeNulls().create();

Foo foo = new Foo();

String json = gson.toJson(foo); //转换后的Json为{"s":null, "i":5}

json = gson.toJson(null);       //转换后的JSON为null


四、GSON的其它功能

   至此把GSON的大体功能已介绍完毕,除此之外还有像“版本支持”、“字段命名绑定”等没有做说明,这并影响对GSON的使用,更多的详情可参见其官网或附件。

   特别声明一点,在实际应用过程中JSON和JAVA相互间的转换,使用泛型的机率很大,曾在www.iteye.com上看过这么一个帖子,如何把下面的JSON对象转换为JAVA对象,请感兴趣的读者参见上面的讲述完成此JSON对象的转换。

[

   {

       "id": 1,

       "pid": 0,

       "name": "总部",

       "open": true,

       "list":[

                  {"id":2, "pid":1, "name":"研发部", "isParent":false, "open":false},

                  {"id":3, "pid":1, "name':"市场部", "isParent":false, "open":false},

                  {"id":4, "pid":1, "name":"开发部", "isParent":false, "open":false}

              ]

   }

]

本文出自 “青客” 博客,请务必保留此出处http://qingkechina.blog.51cto.com/5552198/1290557

目录
相关文章
|
JSON fastjson Java
FastJson、JackJson 以及 Gson 的区别
FastJson、JackJson 以及 Gson 是 Java 生态圈中三种常用的 Json 解析器,它们均可将 Java 对象序列化为 Json 格式的字符串,也可将 Json 字符串反序列化为 Java 对象。下面我们讨论一下三者在序列化和反序列化操作中的一些区别。
839 0
|
5月前
|
JSON Java API
Gson基本使用
Gson基本使用
65 0
|
5月前
|
JSON 数据格式
gson坑
gson坑
33 0
|
11月前
|
JSON Java API
Gson-更新中
Gson-更新中
125 0
|
11月前
|
JSON Java 数据格式
Gson库的基本使用方法
定义:Gson是Google提供用来java对象和Json数据之间进行映射的库,进行序列化/反序列化。
74 0
|
JSON fastjson Java
scala使用Gson和FastJson解析JSON
kafka传过来的数据好多都是JSON格式,需要对其解析,抽取出应用需要的数据。Gson和FastJson是两个不错的解析工具,以后可能经常会使用到,记录一下,防止以后遗忘。
817 0
|
JSON 数据格式
GSON - 基础篇
GSON - 基础篇
169 0
|
JSON 数据可视化 fastjson
Gson 简单使用姿势小结
关于 Json 序列化的框架可以说比较多了,比如 Spring 默认的 Jackson,国内互联网用的比较多的 FastJson,本文则主要介绍一下 Gson 的简单使用姿势,并不会涉及到不同的 json 框架的性能对比
181 0
Gson 简单使用姿势小结
|
JSON Java 测试技术
Google Gson用法详解
Google Gson用法详解
799 0
Google Gson用法详解
|
JSON 数据格式
Gson解析泛型
做网络请求的时候肯定要封装回调,我这里就传了泛型,但是出了个问题是Gson没办法直接解析泛型,如果直接解析的话,不会得到一个javabean而是得到一个LinkedTreeMap。
1675 0