1. 程式人生 > >gson解析工具類

gson解析工具類

GsonUtil 需要配置build.gradle     compile 'com.google.code.gson:gson:2.8.1'

import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import com.google.gson.reflect.TypeToken;

import java.util.ArrayList; import java.util.List; import java.util.Map;

/**  * The type Gson util.  */ public class GsonUtil {     //不用建立物件,直接使用Gson.就可以呼叫方法     private static Gson gson = null;     //判斷gson物件是否存在了,不存在則建立物件     static {         if (gson == null) {             //gson = new Gson();             //當使用GsonBuilder方式時屬性為空的時候輸出來的json字串是有鍵值key的,顯示形式是"key":null,而直接new出來的就沒有"key":null的             gson = new Gson();         }     }     //無參的私有構造方法     private GsonUtil() {     }

    /**      * 將物件轉成json格式      *      * @param object the object      * @return String string      */     public static String GsonString(Object object) {         String gsonString = null;         if (gson != null) {             gsonString = gson.toJson(object);         }         return gsonString;     }

    /**      * 將json轉成特定的cls的物件      *      * @param <T>        the type parameter      * @param gsonString the gson string      * @param cls        the cls      * @return the t      */     public static <T> T GsonToBean(String gsonString, Class<T> cls) {         T t = null;         if (gson != null) {             //傳入json物件和物件型別,將json轉成物件             t = gson.fromJson(gsonString, cls);         }         return t;     }

    /**      * Gson to list list.      *      * @param <T>        the type parameter      * @param jsonString the json string      * @param cls        the cls      * @return the list      */     public static <T> List<T> GsonToList(String jsonString,Class<T> cls){         List<T> list = new ArrayList<T>();         try {             Gson gson = new Gson();             JsonArray arry = new JsonParser().parse(jsonString).getAsJsonArray();             for (JsonElement jsonElement : arry) {                 list.add(gson.fromJson(jsonElement, cls));             }         } catch (Exception e) {             e.printStackTrace();         }         return list;     }

    /**      * json字串轉成list中有map的      *      * @param <T>        the type parameter      * @param gsonString the gson string      * @return the list      */     public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {         List<Map<String, T>> list = null;         if (gson != null) {             list = gson.fromJson(gsonString,                     new TypeToken<List<Map<String, T>>>() {                     }.getType());         }         return list;     }

    /**      * json字串轉成map的      *      * @param <T>        the type parameter      * @param gsonString the gson string      * @return the map      */     public static <T> Map<String, T> GsonToMaps(String gsonString) {         Map<String, T> map = null;         if (gson != null) {             map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {             }.getType());         }         return map;     } }

以上程式碼來自網路自己經常使用忘記從哪裡來的了