Gson转换 — json数据转换为Object实体
最近在做远程接口调用时,需要对数据进行加解密,因此返回的参数统一为以下json格式
{
"code":0,
"msg":"成功",
"sign":"abcdefghijkl",
"timestamp":1491897441000,
"data":{}
}
首先,我们假设传输过程中的对象实体是
user.java
public class User {
private int id;
private String name;
...省略get set...
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
当返回的数据转成对象时,可能存在三种情况:
1. User 对象
2. List<User> list 对象
3. Page page
Page.java
public class Page<T> {
private int page;
private int size;
private int total;
private List<T> data;
...省略 get set...
@Override
public String toString() {
return "User [page=" + page + ", size=" + size + ", total=" + total + ", data=" + data + "]";
}
}
因此返回data的json可能为以下三种:
1.对象实体的json json -> User
{
"id":1,
"name":"eric"
}
2.对象实体列表的json json -> List<User> list
[
{
"id":1,
"name":"eric"
},
{
"id":2,
"name":"john"
}
]
3.分页的实体json json -> Page<User> page
{
"page":1,
"size":10,
"total":2,
"data":[
{
"id":1,
"name":"eric"
},
{
"id":2,
"name":"john"
}
]
}
因此我们需要写一个公共类解决上述转换问题
GsonUtils.java
public class GsonUtils {
private GsonUtils() {
}
public static <T>T fromJson(String json,Class<T> type){
Gson gson = new Gson();
return gson.fromJson(json,type);
}
public static <T> List<T> listFromJson(String json){
Gson gson = new Gson();
return gson.fromJson(json, new TypeToken<List<T>>(){}.getType());
}
}
测试:
public class Main {
public static void main(String[] args) {
String json1 = "{\"id\":1,\"name\":\"eric\"}";
String json2 = "[{\"id\":1,\"name\":\"eric\"},{\"id\":2,\"name\":\"john\"}]";
String json3 = "{\"page\":1,\"size\":10,\"total\":2,\"data\":[{\"id\":1,\"name\":\"eric\"},{\"id\":2,\"name\":\"john\"}]}";
User user = GsonUtils.fromJson(json1, User.class);
List<User> lists = GsonUtils.listFromJson(json2);
Page<User> page = GsonUtils.fromJson(json3, Page.class);
System.out.println("user:" + user);
System.out.println("lists:" + lists);
System.out.println("page:" + page);
}
}
运行后的结果:
user:User [id=1, name=eric]
lists:[{id=1.0, name=eric}, {id=2.0, name=john}]
page:User [page=1, size=10, total=2, list=[{id=1.0, name=eric}, {id=2.0, name=john}]]
此时已经成功将三种格式的json转化为Object,但是这里有个问题如下:如果我们用一下方法获取user,就会报错,因为处理Java反射TypeToken泛型运行时类型擦除
User user = lists.get(0);
Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to swun.test.entity.User
at swun.test.Main.main(Main.java:27)
因此我们将方法listFromJson修改为
public static <T> List<T> listFromJson(String json,Type type){
Gson gson = new Gson();
return gson.fromJson(json, type);
}
在调用的时候,将具体的Type传入
public class Main {
public static void main(String[] args) {
String json1 = "{\"id\":1,\"name\":\"eric\"}";
String json2 = "[{\"id\":1,\"name\":\"eric\"},{\"id\":2,\"name\":\"john\"}]";
String json3 = "{\"page\":1,\"size\":10,\"total\":2,\"data\":[{\"id\":1,\"name\":\"eric\"},{\"id\":2,\"name\":\"john\"}]}";
User user = GsonUtils.fromJson(json1, User.class);
List<User> lists = GsonUtils.listFromJson(json2,new TypeToken<List<User>>(){}.getType());
Page<User> page = GsonUtils.fromJson(json3, Page.class);
System.out.println("user:" + user);
System.out.println("lists:" + lists);
System.out.println("page:" + page);
User user1 = lists.get(0);
System.out.println("user1:" + user1);
}
}
运行结果如下:
user:User [id=1, name=eric]
lists:[User [id=1, name=eric], User [id=2, name=john]]
page:User [page=1, size=10, total=2, data=[{id=1.0, name=eric}, {id=2.0, name=john}]]
user1:User [id=1, name=eric]