使用jackson将json转换为对象时,自定义转换字段内容


前言

本文中采用的是继承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);
    }
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值