在实际业务中,经常遇到需要将json字符串转换为想要的对象,又或者将想要的对象转为json字符串返回或者给其他业务使用。经过总结归纳,编写了一下工具类。有需要使用到朋友,直接拿走使用,不谢。
工具类支持多层json格式数据转换,比如:json字符串中包含多重对象,可以将多重对象转换出想要的字符串
一共分两个类,JsonUtils 和 JsonMapper,可以将json字符串转换为实体类,或者Map对象,
转换为list集合,又或者将以上操作反向转换为json字符串。
1.JsonMapper
package com.common.utils;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.JSONPObject;
public class JsonMapper {
private ObjectMapper mapper;
public JsonMapper() {
this(null);
}
public JsonMapper(Include include) {
mapper = new ObjectMapper();
if (include != null) {
mapper.setSerializationInclusion(include);
}
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
public static JsonMapper nonEmptyMapper() {
return new JsonMapper(Include.NON_EMPTY);
}
public static JsonMapper nonDefaultMapper() {
return new JsonMapper(Include.NON_DEFAULT);
}
public static JsonMapper nonAlwaysMapper() {
return new JsonMapper(Include.ALWAYS);
}
public String toJson(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (IOException e) {
return null;
}
}
public <T> T fromMapToObject(Object map, Class<T> cls) {
return this.fromJson(this.toJson(map), cls);
}
public <T> T fromMapToObject(Object map, JavaType cls) {
return this.fromJson(this.toJson(map), cls);
}
public <T> T fromJson(String jsonString, Class<T> clazz) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return mapper.readValue(jsonString, clazz);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public <T> T fromJson(String jsonString, JavaType javaType) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return (T) mapper.readValue(jsonString, javaType);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public JavaType createCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
public <T> T update(String jsonString, T object) {
try {
return (T) mapper.readerForUpdating(object).readValue(jsonString);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
public void enableEnumUseToString() {
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
}
public ObjectMapper getMapper() {
return mapper;
}
}
2.JsonUtils
package com.common.utils;
import java.util.ArrayList;
import java.util.List;
public class JsonUtils {
protected static JsonMapper jmapper = JsonMapper.nonEmptyMapper();
/**
* json数据结构转换对象
* @param jsonStr
* @param clazz
* @return
*/
public static <T> T fromJson(String jsonStr,Class<T> clazz){
return jmapper.fromJson(jsonStr, clazz);
}
/**json数据结构转换List<MyBean>
* @param jsonStr
* @param clazz
* @return
*/
public static <T> List<T> fromJson2List(String jsonStr,Class<T> clazz){
return jmapper.fromJson(jsonStr,
JsonMapper.nonDefaultMapper().createCollectionType(List.class, clazz));
}
/**
* 对象转换json数据结构
* @param obj
* @return
*/
public static String toJson(Object obj){
return jmapper.toJson(obj);
}
}