将 “【1,2,3】” 转化为List [1,2,3]
public static <T> T json2Object(String json, TypeReference<T> reference, boolean failOnUnknownProperties) {
if (StringUtils.isBlank(json)) {
return null;
}
if (failOnUnknownProperties) {
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
try {
T t = MAPPER.readValue(json, reference);
if (failOnUnknownProperties) {
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
}
return t;
} catch (IOException e) {
log.error("数据 [{}] 反序列化失败", json);
throw new BusinessException(JsonResultCode.CODE_JACKSON_DESERIALIZATION_FAILED);
}
}
使用方式
String arr = "[1,2,3]";
List<Integer> json2Object = Util.json2Object(arr,new TypeReference<List<Integer>>(){}, false);
System.out.println(json2Object);