Gson-序列化案例
本文介绍如何使用Gson将数组,集合和泛型进行序列化/反序列化。
1 Gson处理数组
1.1 编写核心类
MainApp:
package com.yiidian.gson;
import com.google.gson.Gson;
import java.util.Arrays;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class MainApp {
public static void main(String args[]) {
Gson gson = new Gson();
int[] marks = {100,90,85};
String[] names = {"eric","jack","rose"};
//序列化
System.out.print("{");
System.out.print("marks:" + gson.toJson(marks) + ",");
System.out.print("names:" + gson.toJson(names));
System.out.println("}");
//反序列化
marks = gson.fromJson("[100,90,85]", int[].class);
names = gson.fromJson("[\"eric\",\"jack\",\"rose\"]", String[].class);
System.out.println("marks:" + Arrays.toString(marks));
System.out.println("names:" + Arrays.toString(names));
}
}
1.2 运行测试
2 Gson处理集合
2.1 编写核心类
MainApp:
package com.yiidian.gson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class MainApp {
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);
//序列化
System.out.print("{");
System.out.print("marks:" + gson.toJson(marks));
System.out.println("}");
//反序列化
Type listType = new TypeToken<Collection<Integer>>(){}.getType();
marks = gson.fromJson("[100,90,85]", listType);
System.out.println("marks:" +marks);
}
}
2.2 运行测试
3 Gson处理泛型
3.1 编写核心类
MainApp:
package com.yiidian.gson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class MainApp {
public static void main(String args[]) {
//创建Shape对象
Shape<Circle> shape = new Shape<Circle>();
//创建Circle对象
Circle circle = new Circle(5.0);
//把circle对象赋值给shape
shape.setShape(circle);
Gson gson = new Gson();
Type shapeType = new TypeToken<Shape<Circle>>() {}.getType();
//序列化
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);
}
}
3.2 运行测试
热门文章
优秀文章