import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.var;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class JsonUtil {
private static final ObjectMapper MAPPER = new ObjectMapper();
static {
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
/**
* 去除json值前后空格
*
* @param jsonStr jsonStr
* @return
*/
public static JSONObject jsonTrim(String jsonStr) {
return jsonTrim(JSONObject.parseObject(jsonStr));
}
/**
* 去除value的空格
*
* @param jsonObject jsonObject
* @return
*/
public static JSONObject jsonTrim(JSONObject jsonObject) {
Iterator<Map.Entry<String, Object>> iterator = jsonObject.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> next = iterator.next();
Object value = next.getValue();
if (value != null) {
if (value instanceof String) {
//清空值前后空格
jsonObject.put(next.getKey(), ((String) value).trim());
} else if (value instanceof JSONObject) {
jsonTrim((JSONObject) value);
} else if (value instanceof JSONArray) {
jsonTrimArray((JSONArray) value);
}
}
}
return jsonObject;
}
/**
* 清空JSONArray 值前后空格
*
* @param array
*/
private static void jsonTrimArray(JSONArray array) {
if (array.size() > 0) {
for (int i = 0; i < array.size(); i++) {
Object object = array.get(i);
if (object != null) {
if (object instanceof String) {
array.set(i, ((String) object).trim());
} else if (object instanceof JSONObject) {
jsonTrim((JSONObject) object);
} else if (object instanceof JSONArray) {
jsonTrimArray((JSONArray) object);
}
}
}
}
}
// 序列化对象
public static String serialize(Object object) {
try {
return MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static String readField(String content, String fieldName) {
try {
var tree = MAPPER.readTree(content);
if (!tree.has(fieldName)) {
throw new Exception("");
}
return tree.get(fieldName).asText();
} catch (Exception e) {
return null;
}
}
public static String readField(String content) {
return readField(content, "type");
}
// 反序列化对象
public static <T> T deserialize(String json, Class<T> clazz) {
try {
return MAPPER.readValue(json, clazz);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static <T> List<T> deserializeList(String json, TypeReference<List<T>> typeReference) {
try {
return MAPPER.readValue(json, typeReference);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static <K, V> Map<K, V> deserializeMap(String json, TypeReference<Map<K, V>> typeReference) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, typeReference);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static JavaType getCollectionType(Class<?> collectionClass,Class<?>... elementClasses){
return MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
public static <T> T deserialize(String json ,JavaType beanClass){
try {
return MAPPER.readValue(json, beanClass);
}catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static <T> T deserializeCollection(String json ,Class<?> collectionClass,Class<?>... elementClasses){
return deserialize(json,getCollectionType(collectionClass,elementClasses));
}
}
03-17
435
03-22
278
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交