package com.hdl.sdk.hdl_core.Util.TransformUtil;
|
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.JsonElement;
|
import com.google.gson.JsonParser;
|
import com.google.gson.reflect.TypeToken;
|
|
import java.util.List;
|
|
public class JsonUtil {
|
|
public static String toJson(Object object) {
|
GsonBuilder gsonBuilder = new GsonBuilder();
|
gsonBuilder.setPrettyPrinting();
|
Gson gson = gsonBuilder.create();
|
return gson.toJson(object);
|
}
|
/**
|
*解析字符串字典
|
*/
|
public static <T> T parseJsonWithGson(String json, Class<T> type) {
|
|
T result = new Gson().fromJson(json, type);
|
return result;
|
}
|
|
/**
|
* 解析数组
|
*/
|
public static <T> List<T> parseArrayJsonWithGson(String json, Class<T> type) {
|
List<T> list = new Gson().fromJson(json, new TypeToken<List<T>>(){}.getType());
|
return list;
|
}
|
|
/**
|
*json判断
|
*/
|
public static boolean isJson(String content) {
|
JsonElement jsonElement;
|
try {
|
jsonElement = new JsonParser().parse(content);
|
} catch (Exception e) {
|
return false;
|
}
|
if (jsonElement == null) {
|
return false;
|
}
|
if (!jsonElement.isJsonObject()) {
|
return false;
|
}
|
return true;
|
}
|
}
|