/** * 使用谷歌Gson实现Json串和Java Bean互转 */ public class JsonHelper { public static String toJson(Object src){ return new Gson().toJson(src); } public static <T> T fromJson(String json, Class<T> clazz) throws Exception { return new Gson().fromJson(json, clazz); } public static <T> List<T> fromJsonArray(String json, Class<T> clazz) throws Exception { List<T> lst = new ArrayList<T>(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); for(final JsonElement elem : array){ lst.add(new Gson().fromJson(elem, clazz)); } return lst; } }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10338.html