工具(九):JSON操作工具类

    JSON作为一种数据格式,很是流行,无论是在web开发还是app接口开发中,都有很广泛的使用。而且也有很多工具。如阿里巴巴的fastjson,google的gson以及json-lib和jackson等等。都比较好用。下面我们来介绍用jackson作为基础的工具类,上代码:

package com.xxx.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletResponse;

/**
 *  JSON工具类
 */

public class JsonUtil {

    private static ObjectMapper mapper = new ObjectMapper();

    /**
     * 将对象转换为JSON字符串
     *
     * @param object 对象
     */
    public static String toJson(Object object) {
        Assert.notNull(object);
        try {
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将对象转换为JSON流
     *
     * @param response    HttpServletResponse
     * @param contentType contentType
     */
    public static void toJson(HttpServletResponse response, String contentType, Object value) {
        Assert.notNull(response);
        Assert.notNull(contentType);
        Assert.notNull(value);
        try {
            response.setContentType(contentType);
            mapper.writeValue(response.getWriter(), value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将对象转换为JSON流
     *
     * @param response HttpServletResponse
     */
    public static void toJson(HttpServletResponse response, Object value) {
        Assert.notNull(response);
        Assert.notNull(value);
        try {
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            mapper.writeValue(response.getWriter(), value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将JSON字符串转换为对象
     *
     * @param json      JSON字符串
     * @param valueType 对象类型
     */
    public static <T> T toObject(String json, Class<T> valueType) {
        Assert.notNull(json);
        Assert.notNull(valueType);
        try {
            return mapper.readValue(json, valueType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将JSON字符串转换为对象
     *
     * @param json          JSON字符串
     * @param typeReference 对象类型
     */
    public static <T> T toObject(String json, TypeReference<?> typeReference) {
        Assert.notNull(json);
        Assert.notNull(typeReference);
        try {
            return mapper.readValue(json, typeReference);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将JSON字符串转换为对象
     *
     * @param json     JSON字符串
     * @param javaType 对象类型
     */
    public static <T> T toObject(String json, JavaType javaType) {
        Assert.notNull(json);
        Assert.notNull(javaType);
        try {
            return mapper.readValue(json, javaType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

    以上就是代码了。希望帮到大家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一掬净土

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值