Jsonutil工具类(好用)

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.var;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class JsonUtil {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    static {
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    /**
     * 去除json值前后空格
     *
     * @param jsonStr jsonStr
     * @return
     */
    public static JSONObject jsonTrim(String jsonStr) {
        return jsonTrim(JSONObject.parseObject(jsonStr));
    }

    /**
     * 去除value的空格
     *
     * @param jsonObject jsonObject
     * @return
     */
    public static JSONObject jsonTrim(JSONObject jsonObject) {
        Iterator<Map.Entry<String, Object>> iterator = jsonObject.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            Object value = next.getValue();
            if (value != null) {
                if (value instanceof String) {
                    //清空值前后空格
                    jsonObject.put(next.getKey(), ((String) value).trim());
                } else if (value instanceof JSONObject) {
                    jsonTrim((JSONObject) value);
                } else if (value instanceof JSONArray) {
                    jsonTrimArray((JSONArray) value);
                }
            }
        }

        return jsonObject;
    }

    /**
     * 清空JSONArray 值前后空格
     *
     * @param array
     */
    private static void jsonTrimArray(JSONArray array) {
        if (array.size() > 0) {
            for (int i = 0; i < array.size(); i++) {
                Object object = array.get(i);
                if (object != null) {
                    if (object instanceof String) {
                        array.set(i, ((String) object).trim());
                    } else if (object instanceof JSONObject) {
                        jsonTrim((JSONObject) object);
                    } else if (object instanceof JSONArray) {
                        jsonTrimArray((JSONArray) object);
                    }
                }
            }
        }
    }

    // 序列化对象
    public static String serialize(Object object) {
        try {
            return MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String readField(String content, String fieldName) {
        try {
            var tree = MAPPER.readTree(content);
            if (!tree.has(fieldName)) {
                throw new Exception("");
            }
            return tree.get(fieldName).asText();
        } catch (Exception e) {
            return null;
        }
    }

    public static String readField(String content) {
        return readField(content, "type");
    }

    // 反序列化对象
    public static <T> T deserialize(String json, Class<T> clazz) {
        try {
            return MAPPER.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> deserializeList(String json, TypeReference<List<T>> typeReference) {
        try {
            return MAPPER.readValue(json, typeReference);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static <K, V> Map<K, V> deserializeMap(String json, TypeReference<Map<K, V>> typeReference) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(json, typeReference);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static JavaType getCollectionType(Class<?> collectionClass,Class<?>... elementClasses){
        return MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    public static <T> T deserialize(String json ,JavaType beanClass){
        try {
            return MAPPER.readValue(json, beanClass);
        }catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T deserializeCollection(String json ,Class<?> collectionClass,Class<?>... elementClasses){
        return deserialize(json,getCollectionType(collectionClass,elementClasses));
    }
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值