JackJson对象转化

  • 当接受jsonKey首字母为大写的时候需要用JSONProperty 配合 JsonIngore处理
package com.example.demoboot.dto;

import com.example.demoboot.entity.Person;

import java.util.List;

/**
 * 封装response 返回的data对象
 对象太多可以用宽对象,把所有需要的都写一起
 *
 * @author hello
 */
public class CommonResponseDataDto {

    private List<Person> list;
    
    @JsonProperty(value = "P1")
    private Person p1;

    public List<Person> getList() {
        return list;
    }

    public void setList(List<Person> list) {
        this.list = list;
    }

    @JsonIgnore
    public Person getP1() {
    return p1;
    }

    @JsonIgnore
    public void setP1(Person p1) {
    this.p1 = p1;
    }
}
  • JackJson Object对象转Bean (这里指CommonResponseDataDto)
  • 工具类
package com.example.demoboot.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author hello
 */
public class JackJsonUtil {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    /**
     * json数组字符串转java list
     *
     * @param jsonArray     json
     * @param typeReference reference
     * @param <T>           t
     * @return list
     */
    public static <T> T toJavaObjectList(String jsonArray, TypeReference<T> typeReference) {
        T t = null;
        try {
            t = OBJECT_MAPPER.readValue(jsonArray, typeReference);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * json 数组字符串转java list
     *
     * @param object        object
     * @param typeReference reference
     * @param <T>           t
     * @return list
     */
    public static <T> T toJavaObjectList(Object object, TypeReference<T> typeReference) {
        String jsonArray;
        if (object instanceof String) {
            jsonArray = object.toString();
        } else {
            jsonArray = formatJsonString(object);
        }
        return toJavaObjectList(jsonArray, typeReference);
    }

    /**
     * 转json字符串为Java 对象
     *
     * @param jsonString jsonString
     * @param tClass     Bean 类型
     * @param <T>        Class
     * @return java 对象
     */
    public static <T> T toJavaObject(String jsonString, Class<T> tClass) {
        T t = null;
        try {
            t = OBJECT_MAPPER.readValue(jsonString, tClass);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return t;
    }


    /**
     * 转任意对象为Java对象
     *
     * @param object object
     * @param tClass Bean 类型
     * @param <T>    Class
     * @return java 对象
     */
    public static <T> T toJavaObject(Object object, Class<T> tClass) {
        String json;
        if (object instanceof String) {
            json = object.toString();
        } else {
            json = formatJsonString(object);
        }
        return toJavaObject(json, tClass);
    }

    /**
     * 将Object对象转化为jsonString
     *
     * @param o 任意对象
     * @return json字符串
     */
    public static String formatJsonString(Object o) {
        try {
            return OBJECT_MAPPER.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

测试:

String str = "[{\n" +
        "\"name\":\"abc\",\n" +
        "\"age\":23\n" +
        "\n" +
        "},{\n" +
        "\"name\":\"jin\",\n" +
        "\"age\":44\n" +
        "}]";
List<Person> peoples = JackJsonUtil.toJavaObjectList(str, new TypeReference<>() {
});
List<Person> p = JackJsonUtil.toJavaObjectList(peoples, new TypeReference<>() {
});

 

posted @ 2024-03-27 18:01  洞玄巅峰  阅读(70)  评论(0编辑  收藏  举报