2019独角兽企业重金招聘Python工程师标准>>>
pom.xml
<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.7</version>
</dependency>
编写GsonUtils类
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//package util;import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;public class GsonUtils {private static final String EMPTY_JSON = "{}";private static final String EMPTY_JSON_ARRAY = "[]";private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";public GsonUtils() {}public static String toJson(Object target) {return toJson(target, (Type)null, (String)null);}public static String toJson(Object target, Type targetType, String datePattern) {if(target == null) {return EMPTY_JSON;} else {GsonBuilder builder = new GsonBuilder();if(datePattern == null || datePattern.length() < 1) {datePattern = DEFAULT_DATE_PATTERN;}builder.setDateFormat(datePattern);Gson gson = builder.create();String result = EMPTY_JSON;try {if(targetType == null) {result = gson.toJson(target);} else {result = gson.toJson(target, targetType);}} catch (Exception var7) {if(target instanceof Collection || target instanceof Iterator || target instanceof Enumeration || target.getClass().isArray()) {result = EMPTY_JSON_ARRAY;}}return result;}}public static String toJson(Object target, Type targetType) {return toJson(target, targetType, (String)null);}public static <T> T fromJson(String json, TypeToken<T> token, String datePattern) {if(json != null && json.length() >= 1) {GsonBuilder builder = new GsonBuilder();if(datePattern == null || datePattern.length() < 1) {datePattern = DEFAULT_DATE_PATTERN;}builder.setDateFormat(datePattern);Gson gson = builder.create();try {return gson.fromJson(json, token.getType());} catch (Exception var6) {return null;}} else {return null;}}public static Object fromJson(String json, Type type, String datePattern) {if(json != null && json.length() >= 1) {GsonBuilder builder = new GsonBuilder();if(datePattern == null || datePattern.length() < 1) {datePattern = DEFAULT_DATE_PATTERN;}builder.setDateFormat(datePattern);Gson gson = builder.create();try {return gson.fromJson(json, type);} catch (Exception var6) {return null;}} else {return null;}}public static Object fromJson(String json, Type type) {return fromJson(json, (Type)type, (String)null);}public static <T> T fromJson(String json, TypeToken<T> token) {return (T)fromJson(json, (TypeToken)token, (String)null);}public static <T> T fromJson(String json, Class<T> clazz, String datePattern) {if(json != null && json.length() >= 1) {GsonBuilder builder = new GsonBuilder();if(datePattern == null || datePattern.length() < 1) {datePattern = DEFAULT_DATE_PATTERN;}builder.setDateFormat(datePattern);Gson gson = builder.create();try {return gson.fromJson(json, clazz);} catch (Exception var6) {return null;}} else {return null;}}public static <T> T fromJson(String json, Class<T> clazz) {return (T)fromJson(json, (Class)clazz, (String)null);}}
编写test测试类
public class Test {class Model{private String id;private String name;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String toString(){return "id="+this.id+";name="+this.name;}}public static void main(String[] args) {Map<String,Object> map = new HashMap<String,Object>();map.put("1",1);map.put("2",2);map.put("3",3);System.out.println(GsonUtils.toJson(map));List<Model> listTojson = new ArrayList<Model>();Test.Model model1 = new Test().new Model();model1.setId("1");model1.setName("张三");Test.Model model2 = new Test().new Model();model2.setId("2");model2.setName("李四");listTojson.add(model1);listTojson.add(model2);String json = GsonUtils.toJson(listTojson);System.out.println("json="+json);List<Model> modelList = (List<Model>)GsonUtils.fromJson(json,new TypeToken<List<Model>>(){}.getType());System.out.println("modelList="+modelList.toString());}
}