Gson转换 — json数据转换为Object实体公共方法
生活随笔
收集整理的這篇文章主要介紹了
Gson转换 — json数据转换为Object实体公共方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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...@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + "]";}}當返回的數據轉成對象時,可能存在三種情況:
1. User 對象 2. List<User> list 對象 3. Page pagePage.java
public class Page<T> {private int page;private int size;private int total;private List<T> data;...省略 get set...@Overridepublic 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.Userat 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]總結
以上是生活随笔為你收集整理的Gson转换 — json数据转换为Object实体公共方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: swift 学习- 12 -- 方法
- 下一篇: Ajax监测开始执行及结束执行