Json常用序列化工具
1.maven
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
2.Json序列化工具类
1.对象转Json字符串
2.Json字符串转为对象
3.json列表字符串转List对象
4.json字符串转Map对象
5.单个对象或者list的Json字符串都转换成List对象
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 常用Json序列化
*/
public class JsonUtil {
private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);
/**
* 对象转Json字符
*
* @param obj 对象
* @return String
*/
public static String objToJsonStr(Object obj) {
try {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(obj);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* Json字符串转为对象
*
* @param jsonStr
* @param clazz
* @param <T> 对象类型
* @return 对象
*/
public static <T> T jsonStrToObject(String jsonStr, Class<T> clazz) {
try {
if (StringUtil.isEmpty(jsonStr)) return null;
ObjectMapper objectMapper = new ObjectMapper();
//允许未带引号字段
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//允许使用单引号
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return objectMapper.readValue(jsonStr, clazz);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* json列表字符串转List对象
*
* @param jsonStr
* @param clszz
* @param <T>
* @return
*/
public static <T> List<T> jsonToList(String jsonStr, Class<T> clszz) {
try {
if (StringUtil.isEmpty(jsonStr)) return null;
ObjectMapper objectMapper = new ObjectMapper();
//允许未带引号字段
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//允许使用单引号
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return objectMapper.readValue(jsonStr, getCollectionType(objectMapper, List.class, clszz));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Map json字符串转Map对象
*
* @param jsonStr
* @return
*/
public static Map<String, Object> jsonToMap(String jsonStr) {
try {
if (StringUtil.isEmpty(jsonStr)) return null;
ObjectMapper objectMapper = new ObjectMapper();
//允许未带引号字段
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//允许使用单引号
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return objectMapper.readValue(jsonStr, new TypeReference<Map<String, Object>>() {
});
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取泛型的Collection Type
*
* @param collectionClass 泛型的Collection
* @param elementClasses 实体bean
* @return JavaType Java类型
*/
private static JavaType getCollectionType(ObjectMapper mapper, Class<?> collectionClass, Class<?>... elementClasses) {
return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
/**
* 单个对象或者list的Json字符串都转换成List对象
*
* @param jsonStr
* @param cla
* @param <T>
* @return
*/
public static <T> List<T> objOrArrToList(String jsonStr, Class<T> cla) {
if (StringUtil.isEmpty(jsonStr)) return null;
//单个或多个、都转成list
boolean single = jsonStr.startsWith("{");
List<T> cList = null;
if (single) {
T t = jsonStrToObject(jsonStr, cla);
cList = new ArrayList();
cList.add(t);
} else {
cList = jsonToList(jsonStr, cla);
}
return cList;
}
}