工具(九):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; }}
以上就是代码了。希望帮到大家。
阅读全文
1 0
- 工具(九):JSON操作工具类
- JSON操作工具类
- java JSON 操作 工具类
- java操作json工具类
- java操作Json工具类
- 操作json的工具类
- Java操作JSON的便捷工具类(Gson)
- Java操作JSON的便捷工具类(Gson)
- Java操作JSON的便捷工具类(Gson)
- 电话操作-JSON解析-图片下载工具类
- C#的JSON操作工具类
- Jackson实现的json操作工具类
- JSON工具类(java)
- Java操作JSON工具整理
- String json 字符串操作工具
- 性能测试工具操作数据库(九)-Loadrunner与MongoDB
- JSON-json-lib-工具类
- JSON工具(org.json)
- MySQL(三)
- HTTP Keep-Alive是什么?如何工作?
- MTK flashtool 在14.04上不能使用问题
- Servlet常用接口、作用域及停止方法
- Django开发实践-2
- 工具(九):JSON操作工具类
- Golang JSON Unmarshal MySQL datetime to time.Time
- 记——嵌入式学习开始
- python numpy.save 函数 SystemError: error return without exception set
- 查看apk包名的方法总结
- 使用pip安装第三方包
- GitHub上史上最全的iOS开源项目分类汇总
- 项目中的问题
- Linux基础—source、点(.)和sh命令执行脚本的方式解析