jackson实体转Json互转工具类

目录

工具类

例子:

例1:对象转json 和 json转对象

例2:集合转json和 json转集合


自己基于jackson封装的json转换数据工具类

提供实体转json,json转实体,以及jsonNode的获取方

工具类

package com.example.fl.fuli.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.FilterProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;

public class JsonUtil {

    private static ObjectMapper mapper = new ObjectMapper();
    static {
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
    public static void setDateFormat(String pattern) {
        mapper.setDateFormat(new SimpleDateFormat(pattern));
    }

    public static void setNamingStrategy(PropertyNamingStrategy propertyNamingStrategy) {
        mapper.setPropertyNamingStrategy(propertyNamingStrategy);
    }

    public static void unwrapRootValue() {
        mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
    }

    public static void wrapRootValue() {
        mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
    }

    public static void setFilterProvider(FilterProvider filterProvider) {
        mapper.setFilterProvider(filterProvider);
    }

    public static String objectToJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Failed to convert object to json", e);
        }
    }

    public static <T> T jsonToObject(String responseJson, Class<T> clazz) {
        try {
            return mapper.readValue(responseJson, clazz);
        } catch (IOException e) {
            throw new RuntimeException("Failed to convert json to object", e);
        }
    }

    public static <T> T jsonToObject(String responseJson, TypeReference<T> valueTypeRef) {
        try {
            return mapper.readValue(responseJson, valueTypeRef);
        } catch (IOException e) {
            throw new RuntimeException("Failed to convert json to object", e);
        }
    }

    public static JsonNode getJsonNode(String jsonStr) {
        try {
            return mapper.readTree(jsonStr);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Failed to convert request to JsonNode",
                    e);
        } catch (IOException e) {
            throw new RuntimeException("IO exception", e);
        }
    }

}

例子:

对象:Person

例1:对象转json 和 json转对象

        Person person=new Person();
        person.setName("张三");
        person.setAge(10);
        person.setDescription("张三,十岁了");
        //转换为json
        String json = JsonUtil.objectToJson(person);
        // {"name":"张三","age":10,"description":"张三,十岁了"}
        System.out.println(json);
        //json转换为对象
        Person person1 = JsonUtil.jsonToObject(json, Person.class);
        // Person(name=张三, age=10, description=张三,十岁了),这里应该是在IDEA中转换的问题,实际应该为{"name":"张三","age":10,"description":"张三,十岁了"}
        System.out.println(person1);

例2:集合转json和 json转集合

    Person person=new Person();
        person.setName("张三");
        person.setAge(10);
        person.setDescription("张三,十岁了");
        Person person2=new Person();
        person2.setName("李四");
        person2.setAge(11);
        person2.setDescription("李四,十一岁了");
        Person person3=new Person();
        person3.setName("王五");
        person3.setAge(12);
        person3.setDescription("王五,十二岁了");
        List<Person> list = new ArrayList<Person>();
        list.add(person);
        list.add(person2);
        list.add(person3);
        //集合转json
        String json = JsonUtil.objectToJson(list);
        //[{"name":"张三","age":10,"description":"张三,十岁了"},{"name":"李四","age":11,"description":"李四,十一岁了"},{"name":"王五","age":12,"description":"王五,十二岁了"}]
        System.out.println(json);
        //json转集合(因为知道类型,所以可以直接转换)
        List <Person>object = JsonUtil.jsonToObject(json, List.class);
        //[{"name":"张三","age":10,"description":"张三,十岁了"},{"name":"李四","age":11,"description":"李四,十一岁了"},{"name":"王五","age":12,"description":"王五,十二岁了"}]
        System.out.println(object);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值