package com.whyc.util;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.JsonSyntaxException;
|
|
import java.lang.reflect.Type;
|
|
public class JsonUtil {
|
|
/**
|
*
|
* @param dataType 需要解析的日期的格式如:"yyyy-MM-dd HH:mm:ss"
|
* @return 得到对应的gson对象
|
*/
|
public static Gson getGson(String dataType){
|
return new GsonBuilder().setDateFormat(dataType).create();
|
}
|
|
/**
|
* 获取默认的gson对象
|
* @return
|
*/
|
public static Gson getGson(){
|
return new Gson();
|
}
|
|
public static <T> T getObject(String jsonstring, Type listtype){
|
Gson gson=new Gson();
|
T t=null;
|
try {
|
t=gson.fromJson(jsonstring, listtype);
|
} catch (JsonSyntaxException e) {
|
e.printStackTrace();
|
}
|
return t;
|
}
|
|
}
|