JSON

google Json

字号+ 作者:H5之家 来源:H5之家 2017-04-20 17:00 我要评论( )

gradle仓库地址: // https://mvnrepository.com/artifact/com.google.code.gson/gson compile group:

gradle仓库地址:

// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'

package utils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.internal.$Gson$Types; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; /** * JSON对象映射解析工具类 * * @author audaque * */ public class GsonUtils { public static <T> T getObject(String jsonString, Class<T> cls) { T t = null; try { GsonBuilder builder = new GsonBuilder(); DateDeserializer deserializer = new DateDeserializer(); builder.registerTypeAdapter(Date.class, deserializer); Gson gson = builder.create(); t = gson.fromJson(jsonString, cls); } catch (Exception e) { Logger.getLogger(GsonUtils.class.getName()).log(Level.SEVERE, null, e); } return t; } public static <T> List<T> getObjects(String jsonString, Class<T> cls) { try { GsonBuilder builder = new GsonBuilder(); DateDeserializer deserializer = new DateDeserializer(); builder.registerTypeAdapter(Date.class, deserializer); Gson gson = builder.create(); Type tp = $Gson$Types.newParameterizedTypeWithOwner(null, (Type) List.class, cls); return gson.fromJson(jsonString, tp); } catch (Exception e) { Logger.getLogger(GsonUtils.class.getName()).log(Level.SEVERE, null, e); } return null; } public static List<String> getList(String jsonString) { List<String> list = new ArrayList<String>(); try { Gson gson = new Gson(); list = gson.fromJson(jsonString, new TypeToken<List<String>>() { }.getType()); } catch (Exception e) { Logger.getLogger(GsonUtils.class.getName()).log(Level.SEVERE, null, e); } return list; } public static List<Map<String, Object>> listKeyMap(String jsonString) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); try { Gson gson = new Gson(); list = gson.fromJson(jsonString, new TypeToken<List<Map<String, Object>>>() { }.getType()); } catch (Exception e) { Logger.getLogger(GsonUtils.class.getName()).log(Level.SEVERE, null, e); } return list; } public static String getJsonString(Object obj) { String json = null; try { // Gson gson = new Gson(); GsonBuilder builder = new GsonBuilder(); builder.disableHtmlEscaping(); DateSerializer serializer = new DateSerializer(); builder.registerTypeAdapter(Date.class, serializer); Gson gson = builder.create(); json = gson.toJson(obj); } catch (Exception e) { Logger.getLogger(GsonUtils.class.getName()).log(Level.SEVERE, null, e); } return json; } }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • JSON-lib学习3循环引用

    JSON-lib学习3循环引用

    2017-04-20 17:02

  • ASP.NET MVC 4 中的JSON数据交互的方法

    ASP.NET MVC 4 中的JSON数据交互的方法

    2017-04-20 16:03

  • Qt 学习之路 2(63):使用 QJson 处理 JSON

    Qt 学习之路 2(63):使用 QJson 处理 JSON

    2017-04-19 08:03

  • 使用Python解析JSON详解

    使用Python解析JSON详解

    2017-04-18 16:02

网友点评