package com.panda.change.util; import java.lang.reflect.Type; import com.google.gson.Gson; /** * java對象和Json字符串轉化工具類 * @author think */ public final class JsonUtil { private JsonUtil() { } /** * 對象轉化成json字符串 * @param obj * @return */ public static String toJson(Object obj){ Gson gson = new Gson(); return gson.toJson(obj); } /** * json字符串轉成對象 * @param str * @param type * @return */ public static <T> T fromJson(String str,Type type){ Gson gson = new Gson(); return gson.fromJson(str, type); } /** * json字符串轉成對象 * @param str * @param type * @return */ public static <T> T fromJson(String str, Class<T> type) { Gson gson = new Gson(); return gson.fromJson(str, type); } }