标签:格式 style tps object dep ora 对象 null code
import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.util.List; import java.util.Map; /** * <!--GSON--> * <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> * <dependency> * <groupId>com.google.code.gson</groupId> * <artifactId>gson</artifactId> * <version>2.8.5</version> * </dependency> * GSON工具类 */ public class utilJSON { private static Gson gson = null; static { if (gson == null) { gson = new Gson(); } } private String status; private Object data; public utilJSON(String status, Object data) { this.status = status; this.data = data; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Object getObject() { return data; } public void setObject(Object object) { this.data = object; } /** * 对象转json格式 * * @param status 状态 * @param object 对象 * @return */ public static String result(String status, Object object) { utilJSON json = new utilJSON(status, object); String res = null; if (gson != null) { res = gson.toJson(json); } return res; } /** * Json转成对象 * * @param json * @param cls * @return 对象 */ public static <T> T gsonToBean(String json, Class<T> cls) { T t = null; if (gson != null) { t = gson.fromJson(json, cls); } return t; } /** * json转成list中有map的 * * @param json * @return List<Map<String, T>> */ public static <T> List<Map<String, T>> gsonToListMaps(String json) { List<Map<String, T>> list = null; if (gson != null) { list = gson.fromJson(json, new TypeToken<List<Map<String, T>>>() { }.getType()); } return list; } /** * json转成map的 * * @param json * @return Map<String, T> */ public static <T> Map<String, T> gsonToMaps(String json) { Map<String, T> map = null; if (gson != null) { map = gson.fromJson(json, new TypeToken<Map<String, T>>() { }.getType()); } return map; } }
标签:格式 style tps object dep ora 对象 null code
原文地址:https://www.cnblogs.com/xymaxbf/p/12057078.html