GsonUtil.java
package com.sunrise.jop.common.util;import java.lang.reflect.Type;import java.util.HashMap;import java.util.List;import java.util.Map;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;public final class GsonUtil {private GsonUtil(){} /** * 對象轉換成json字串 * @param obj * @return */ public static String toJson(Object obj) { try{ Gson gson = new Gson(); return gson.toJson(obj); }catch(Exception e){ e.printStackTrace(); } return null; } /** * 對象轉換成json字串 * @param obj * @return */ public static String toJson(String key ,Object obj) { try{ Map map = new HashMap(); map.put(key, obj); Gson gson = new Gson(); return gson.toJson(map); }catch(Exception e){ e.printStackTrace(); } return null; } /** * json字串轉成對象 * @param str * @param type * @return */ public static Object fromJsonToObject(String str, Type type) { try{ Gson gson = new Gson(); return gson.fromJson(str, type); }catch(Exception e){ e.printStackTrace(); } return null; } /** * json字串轉成對象 * @param str * @param type * @return */ public static Object fromJsonToObject(String str, Class type) { try{ Gson gson = new Gson(); return gson.fromJson(str, type); }catch(Exception e){ e.printStackTrace(); } return null; } /** * json轉List * */ private void test(){ String json = ""; String result = new Gson().fromJson(json,new TypeToken<List<Object>>() {}.getType());//轉換為集合 }}