pom.xml
com.google.code.gson gson 2.7
编写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 staticT fromJson(String json, TypeToken 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 fromJson(String json, TypeToken token) { return (T)fromJson(json, (TypeToken)token, (String)null); } public static T fromJson(String json, Class 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 fromJson(String json, Class 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) { Mapmap = new HashMap (); map.put("1",1); map.put("2",2); map.put("3",3); System.out.println(GsonUtils.toJson(map)); List listTojson = new ArrayList (); 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 modelList = (List )GsonUtils.fromJson(json,new TypeToken
>(){}.getType()); System.out.println("modelList="+modelList.toString()); }}