處理Json工具類GsonKit
GsonKit:使用Google的Gson庫封裝的工具類,專門負責解析Json數據。
使用Maven引入jar包:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.4</version> </dependency>
以下是代碼細節:
public class GsonKit {
private static Gson gson = null;
static {
if (gson == null) {
gson = new Gson();
}
}
/**
* 將對象轉換成json格式
* @param ts
* @return
*/
public static String objectToJson(Object ts) {
String jsonStr = null;
if (gson != null) {
jsonStr = gson.toJson(ts);
}
return jsonStr;
}
/**
* 將對象轉換成json格式(并自定義日期格式)
*
* @param ts
* @return
*/
public static String objectToJson(Object ts, final String dateformat) {
String jsonStr = null;
gson = new GsonBuilder().registerTypeHierarchyAdapter(Date.class, new JsonSerializer<Date>() {
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
SimpleDateFormat format = new SimpleDateFormat(dateformat);
return new JsonPrimitive(format.format(src));
}
}).setDateFormat(dateformat).create();
if (gson != null) {
jsonStr = gson.toJson(ts);
}
return jsonStr;
}
/**
* 將json格式轉換成list對象
* @param jsonStr
* @return
*/
public static List<?> jsonToList(String jsonStr) {
List<?> objList = null;
if (gson != null) {
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<?>>() {}.getType();
objList = gson.fromJson(jsonStr, type);
}
return objList;
}
/**
* 將json格式轉換成list對象,并準確指定類型
* @param jsonStr
* @param type
* @return
*/
public static List<?> jsonToList(String jsonStr, java.lang.reflect.Type type) {
List<?> objList = null;
if (gson != null) {
objList = gson.fromJson(jsonStr, type);
}
return objList;
}
/**
* 將json格式轉換成map對象
* @param jsonStr
* @return
*/
public static Map<?, ?> jsonToMap(String jsonStr) {
Map<?, ?> objMap = null;
if (gson != null) {
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<Map<?, ?>>() {}.getType();
objMap = gson.fromJson(jsonStr, type);
}
return objMap;
}
/**
* 將json轉換成bean對象
* @param jsonStr
* @return
*/
public static Object jsonToBean(String jsonStr, Class<?> cl) {
Object obj = null;
if (gson != null) {
obj = gson.fromJson(jsonStr, cl);
}
return obj;
}
/**
* 將json轉換成bean對象(并自定義日期格式)
* @param jsonStr
* @param cl
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T jsonToBean(String jsonStr, Class<T> cl, final String pattern) {
Object obj = null;
gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
SimpleDateFormat format = new SimpleDateFormat(pattern);
String dateStr = json.getAsString();
try {
return format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}).setDateFormat(pattern).create();
if (gson != null) {
obj = gson.fromJson(jsonStr, cl);
}
return (T) obj;
}
/**
* 將json轉換成bean對象(并自定義日期格式)
* @param jsonReader
* @param cl
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T jsonToBean(Reader jsonReader, Class<T> cl, final String pattern) {
Object obj = null;
gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
SimpleDateFormat format = new SimpleDateFormat(pattern);
String dateStr = json.getAsString();
try {
return format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}).setDateFormat(pattern).create();
if (gson != null) {
obj = gson.fromJson(jsonReader, cl);
}
return (T) obj;
}
/**
* 根據key獲得value值
* @param jsonStr
* @param key
* @return
*/
public static Object getJsonValue(String jsonStr, String key) {
Object rulsObj = null;
Map<?, ?> rulsMap = jsonToMap(jsonStr);
if (rulsMap != null && rulsMap.size() > 0) {
rulsObj = rulsMap.get(key);
}
return rulsObj;
}
} 本文由用戶 peke 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!