前言
本文中采用的是继承JsonDeserializer抽象类 + 注解的方式实现自定义转换字段内容。
实体类
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.qf.utils.json.TestCustomDateDeserializer;
import lombok.Data;
/**
* @author qf
* @since 2024/09/09 19:54
*/
@Data
public class DataBo {
@JsonProperty("name")
@JsonDeserialize(using = TestCustomDateDeserializer.class)
private String name;
private String value;
}
工具类
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
/**
* jackson工具类
*
* @author qf
* @since 2024/09/09 19:56
*/
public class JacksonUtil {
private static class SingletonHolder {
private static ObjectMapper mapper = new ObjectMapper();
static {
// 在构造器中进行配置
//反序列化的时候如果多了其他属性,不抛出异常
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
}
public static ObjectMapper getSingleton() {
ObjectMapper mapper = SingletonHolder.mapper;
return mapper;
}
/**
* 对象转json
*
* @param obj
* @return
* @throws JsonProcessingException
*/
public static String toJson(Object obj) throws JsonProcessingException {
ObjectMapper mapper = getSingleton();
return mapper.writeValueAsString(obj);
}
/**
* json转为对象
*
* @param json
* @param clazz
* @param <T>
* @return
* @throws IOException
*/
public static <T> T toObject(String json, Class<T> clazz) throws IOException {
ObjectMapper mapper = getSingleton();
return mapper.readValue(json, clazz);
}
/**
* 通过字段名拿数据
* jsonNode.get("xxx").toString();
* 如果是数组
* jsonNode.get("xxx").get(0).toString();
* ...
* jsonNode.get("xxx").get(n).toString();
*
* @param json
* @return
* @throws IOException
*/
public static JsonNode getJsonNode(String json) throws IOException {
ObjectMapper mapper = getSingleton();
JsonNode jsonNode = mapper.readTree(json);
return jsonNode;
}
/**
* json转换为目标类型的列表。
*
* @param json JSON字符串
* @param typeReference 目标类型的TypeReference
* @param <T> 泛型类型
* @return 转换后的列表
* @throws IOException 如果读取失败
*/
public static <T> List<T> toList(String json, TypeReference<List<T>> typeReference) throws IOException {
ObjectMapper mapper = getSingleton();
return mapper.readValue(json, typeReference);
}
}
继承JsonDeserializer的子类
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
/**
* @author qf
* @since 2024/09/09 20:03
*/
public class TestCustomDateDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String dateStr = p.getText();
if("zs".equals(dateStr)){
return "zhangsan";
}
return dateStr;
}
}
测试类
import java.io.IOException;
/**
* @author qf
* @since 2024/09/09 20:04
*/
public class Test {
public static void main(String[] args) throws IOException {
String json = "{\"name\":\"zs\",\"value\":\"11\"}";
DataBo bo = JacksonUtil.toObject(json, DataBo.class);
System.out.println(bo);
}
}