在本章中,我们将讨论数组,集合和泛型的序列化/反序列化.
数组示例
int[] marks = {100,90,85}; //Serialization System.out.println("marks:" + gson.toJson(marks)); //De-serialization marks = gson.fromJson("[100,90,85]", int[].class); System.out.println("marks:" + Arrays.toString(marks));
示例
让我们看一下阵列序列化/反序列化的实际应用.在C:\> GSON_WORKSPACE中创建名为 GsonTester 的Java类文件.
文件:GsonTester.java
import java.util.Arrays; import com.google.gson.Gson; public class GsonTester { public static void main(String args[]) { Gson gson = new Gson(); int[] marks = {100,90,85}; String[] names = {"Ram","Shyam","Mohan"}; //Serialization System.out.print("{"); System.out.print("marks:" + gson.toJson(marks) + ","); System.out.print("names:" + gson.toJson(names)); System.out.println("}"); //De-serialization marks = gson.fromJson("[100,90,85]", int[].class); names = gson.fromJson("[Ram,Shyam,Mohan]", String[].class); System.out.println("marks:" + Arrays.toString(marks)); System.out.println("names:" + Arrays.toString(names)); } }
验证结果
使用 javac编译类编译如下 :
C:\ GSON_WORKSPACE> javac GsonTester.java
现在运行GsonTester查看结果 :
C:\ GSON_WORKSPACE> java GsonTester
验证输出.
{marks:[100,90,85],names:["Ram","Shyam","Mohan"]} marks:[100, 90, 85] names:[Ram, Shyam, Mohan]
集合示例
List marks = new ArrayList(); //Serialization System.out.println("marks:" + gson.toJson(marks)); //De-serialization //get the type of the collection. Type listType = new TypeToken<list>(){}.getType(); //pass the type of collection marks = gson.fromJson("[100,90,85]", listType); System.out.println("marks:" +marks);</list>
示例
让我们看看集合序列化/反序列化的实际应用.在C:\> GSON_WORKSPACE中创建名为 GsonTester 的Java类文件.
文件:GsonTester.java
import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonTester { public static void main(String args[]) { Gson gson = new Gson(); Collection<Integer> marks = new ArrayList<Integer>(); marks.add(100); marks.add(90); marks.add(85); //Serialization System.out.print("{"); System.out.print("marks:" + gson.toJson(marks)); System.out.println("}"); //De-serialization Type listType = new TypeToken<Collection<Integer>>(){}.getType(); marks = gson.fromJson("[100,90,85]", listType); System.out.println("marks:" +marks); } }
验证结果
使用 javac编译类编译如下 :
C:\ GSON_WORKSPACE> javac GsonTester.java
现在运行 GsonTester 查看结果 :
C:\ GSON_WORKSPACE> java GsonTester
验证输出.
{marks:[100,90,85]} marks:[100, 90, 85]
泛型示例
Gson使用Java反射API来获取要映射Json文本的对象的类型.但是对于泛型,这些信息在序列化过程中会丢失.为了解决这个问题,Gson提供了一个 com.google.gson.reflect.TypeToken 类来存储通用对象的类型.
示例
让我们看看Generics序列化/反序列化的实际应用.在C:\> GSON_WORKSPACE中创建名为 GsonTester 的Java类文件.
文件:GsonTester.java
import java.lang.reflect.Type; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonTester { public static void main(String args[]) { // create a shape class of type circle. Shape<Circle> shape = new Shape<Circle>(); // Create a Circle object Circle circle = new Circle(5.0); //assign circle to shape shape.setShape(circle); Gson gson = new Gson(); // Define a Type shapeType of type circle. Type shapeType = new TypeToken<Shape<Circle>>() {}.getType(); //Serialize the json as ShapeType String jsonString = gson.toJson(shape, shapeType); System.out.println(jsonString); Shape shape1 = gson.fromJson(jsonString, Shape.class); System.out.println(shape1.get().getClass()); System.out.println(shape1.get().toString()); System.out.println(shape1.getArea()); Shape shape2 = gson.fromJson(jsonString, shapeType); System.out.println(shape2.get().getClass()); System.out.println(shape2.get().toString()); System.out.println(shape2.getArea()); } } class Shape <T> { public T shape; public void setShape(T shape) { this.shape = shape; } public T get() { return shape; } public double getArea() { if(shape instanceof Circle) { return ((Circle) shape).getArea(); } else { return 0.0; } } } class Circle { private double radius; public Circle(double radius){ this.radius = radius; } public String toString() { return "Circle"; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { return (radius*radius*3.14); } }
验证结果
使用 javac编译类编译如下 :
C:\ GSON_WORKSPACE> javac GsonTester.java
现在运行 GsonTester 查看结果 :
C:\ GSON_WORKSPACE> java GsonTester
验证输出.
{"shape":{"radius":5.0}} class com.google.gson.internal.LinkedTreeMap {radius = 5.0} 0.0 class Circle Circle 78.5
免责声明:以上内容(如有图片或视频亦包括在内)有转载其他网站资源,如有侵权请联系删除
-
设计总结|如何更好地表达活动品牌?
-
谈谈“目标思维”的落地
编辑导读:我们在做数据分析之前,一定要搞清楚需求方的目标到底是什么,要根据目标来重新定义业务方提出的问题,这就是目标思维。目标思维有多重要呢?应该如何落地呢...
-
在线教育平台竞品分析:网易云课堂vs腾讯课堂
本文从移动端出发,对当前比较热门的两款在线教育平台软件-网易云课堂和腾讯课堂进行比较和分析,不足之处还请大家多提意见。 市场分析 随着国内互联网技术的发展和移...
-
即学即用|父亲节活动的4种运营策略
-
B端产品经理和体验设计师的工作职责边界梳理
-
豆果美食电商分支用户体验报告及建议
-
数据分析师如何提高工作效率
在我们的日常工作中,提高工作效率是每个岗位都需要实现的,在工作中,面对比较凌乱的事情时,首先我们需要梳理清楚,按重要级进行开展;本文作者分享了关于...
-
2016中国云计算SaaS移动办公平台年度综合报告
-
网易大布局教育事业:网易公开课、网易云课堂和MOOC分析
-
一篇文章搞懂语音交互的来龙去脉