139 lines
4.3 KiB
Java
139 lines
4.3 KiB
Java
package sc545.pay.utils;
|
||
|
||
|
||
import java.lang.reflect.Type;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import com.google.gson.Gson;
|
||
import com.google.gson.GsonBuilder;
|
||
import com.google.gson.JsonParser;
|
||
import com.google.gson.reflect.TypeToken;
|
||
|
||
public class GsonUtils{
|
||
|
||
//线程安全的
|
||
private static final Gson GSON;
|
||
private static final Gson GSON_NULL; // 不过滤空值
|
||
static {
|
||
GSON = new GsonBuilder().enableComplexMapKeySerialization() //当Map的key为复杂对象时,需要开启该方法
|
||
// .serializeNulls() //当字段值为空或null时,依然对该字段进行转换
|
||
// .excludeFieldsWithoutExposeAnnotation()//打开Export注解,但打开了这个注解,副作用,要转换和不转换都要加注解
|
||
.setDateFormat("yyyy-MM-dd HH:mm:ss")//序列化日期格式 "yyyy-MM-dd"
|
||
// .setPrettyPrinting() //自动格式化换行
|
||
.disableHtmlEscaping() //防止特殊字符出现乱码
|
||
.create();
|
||
GSON_NULL = new GsonBuilder().enableComplexMapKeySerialization() //当Map的key为复杂对象时,需要开启该方法
|
||
.serializeNulls() //当字段值为空或null时,依然对该字段进行转换
|
||
// .excludeFieldsWithoutExposeAnnotation()//打开Export注解,但打开了这个注解,副作用,要转换和不转换都要加注解
|
||
.setDateFormat("yyyy-MM-dd HH:mm:ss")//序列化日期格式 "yyyy-MM-dd"
|
||
// .setPrettyPrinting() //自动格式化换行
|
||
.disableHtmlEscaping() //防止特殊字符出现乱码
|
||
.create();
|
||
}
|
||
|
||
//获取gson解析器
|
||
public static Gson getGson() {
|
||
return GSON;
|
||
}
|
||
|
||
//获取gson解析器 有空值 解析
|
||
public static Gson getWriteNullGson() {
|
||
return GSON_NULL;
|
||
}
|
||
|
||
|
||
/**
|
||
* 根据对象返回json
|
||
*/
|
||
public static String ObjectToJson(Object object) {
|
||
return GSON.toJson(object);
|
||
}
|
||
|
||
/**
|
||
* 将字符串转化对象
|
||
*
|
||
* @param json 源字符串
|
||
* @param classOfT 目标对象类型
|
||
* @param <T>
|
||
* @return
|
||
*/
|
||
public static <T> T JsonToObject(String json, Class<T> classOfT) {
|
||
return GSON.fromJson(json, classOfT);
|
||
}
|
||
|
||
/**
|
||
* 将字符串转化对象
|
||
*
|
||
* @param json 源字符串
|
||
* @param t 目标对象类型
|
||
* @param <T>
|
||
* @return
|
||
*/
|
||
public static <T> T JsonToObject(String json, Type t) {
|
||
return GSON.fromJson(json, t);
|
||
}
|
||
|
||
/**
|
||
* 将json转化为对应的实体对象
|
||
* new TypeToken<List<T>>() {}.getType()
|
||
* new TypeToken<Map<String, T>>() {}.getType()
|
||
* new TypeToken<List<Map<String, T>>>() {}.getType()
|
||
*/
|
||
public static <T> T JsonToBean(String json, Type typeOfT) {
|
||
return GSON.fromJson(json, typeOfT);
|
||
}
|
||
|
||
/**
|
||
* 转成list
|
||
* @param gsonString
|
||
* @param cls
|
||
* @return
|
||
*/
|
||
public static <T> List<T> JsonToList(String gsonString, Class<T> cls) {
|
||
return GSON.fromJson(gsonString, new TypeToken<List<T>>() {
|
||
}.getType());
|
||
}
|
||
|
||
/**
|
||
* 转成list中有map的
|
||
* @param gsonString
|
||
* @return
|
||
*/
|
||
public static <T> List<Map<String, T>> JsonToListMaps(String gsonString) {
|
||
return GSON.fromJson(gsonString, new TypeToken<List<Map<String, String>>>() {
|
||
}.getType());
|
||
}
|
||
|
||
/**
|
||
* 转成map
|
||
* @param gsonString
|
||
* @return
|
||
*/
|
||
public static <T> Map<String, T> JsonToMaps(String gsonString) {
|
||
return GSON.fromJson(gsonString, new TypeToken<Map<String, T>>() {
|
||
}.getType());
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 获取json字符串中的某个值
|
||
* @param str json字符串
|
||
* @param key 字段名
|
||
* @param errorKey 若可能返回错误码且没有上述key这个字段,这里写错误码的字段名,否则填null
|
||
* @return 若错误码,返回 "_errorKey_错误码字段值"
|
||
*/
|
||
public static String getJsonValue(String str,String key,String errorKey){
|
||
if(str==null) return "";
|
||
if(errorKey!=null&&str.indexOf(errorKey)>=0)
|
||
return "_"+errorKey+"_"+new JsonParser().parse(str).getAsJsonObject().get(errorKey).getAsString();
|
||
else
|
||
return new JsonParser().parse(str).getAsJsonObject().get(key).getAsString();
|
||
}
|
||
|
||
}
|