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

来源:互联网 发布:unity3d视频教程2017 编辑:程序博客网 时间:2024/06/07 19:49

    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;    }}

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

原创粉丝点击