工具类分享之《JsonUtil》
来源:互联网 发布:无光源网络是什么意思 编辑:程序博客网 时间:2024/05/17 14:28
前言
今天给大家分享的是操作json的工具类,使用的是jackson,如果你使用的是spring boot的话直接引入spring-boot-starter-parent响应的包会自动引入。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent>
或maven引入
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.2</version></dependency><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.2</version></dependency><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.2</version></dependency>
代码
package com.zhuma.demo.util;import java.util.ArrayList;import java.util.List;import org.springframework.util.StringUtils;import com.fasterxml.jackson.annotation.JsonInclude.Include;import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.DeserializationFeature;import com.fasterxml.jackson.databind.ObjectMapper;/** * @desc JSON操作工具类 * * @author zhumaer * @since 6/20/2017 16:37 PM */public class JsonUtil { private static ObjectMapper mapper = new ObjectMapper(); static { mapper.setSerializationInclusion(Include.NON_NULL); //设置输入时忽略JSON字符串中存在而Java对象实际没有的属性 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } public static String object2Json(Object o) { if (o == null) return null; String s = null; try { s = mapper.writeValueAsString(o); } catch (Exception e) { e.printStackTrace(); } return s; } public static <T> List<String> listObject2ListJson(List<T> objects) { if (objects == null) return null; List<String> lists = new ArrayList<String>(); for (T t : objects) { lists.add(JsonUtil.object2Json(t)); } return lists; } public static <T> List<T> listJson2ListObject(List<String> jsons, Class<T> c) { if (jsons == null) return null; List<T> ts = new ArrayList<T>(); for (String j : jsons) { ts.add(JsonUtil.json2Object(j, c)); } return ts; } public static <T> T json2Object(String json, Class<T> c) { if (StringUtils.hasLength(json) == false) return null; T t = null; try { t = mapper.readValue(json, c); } catch (Exception e) { e.printStackTrace(); } return t; } @SuppressWarnings("unchecked") public static <T> T json2Object(String json, TypeReference<T> tr) { if (StringUtils.hasLength(json) == false) return null; T t = null; try { t = (T) mapper.readValue(json, tr); } catch (Exception e) { e.printStackTrace(); } return (T) t; }}
欢迎关注我们的公众号或加群,等你哦!
阅读全文
0 0
- 工具类分享之《JsonUtil》
- JSON工具类JSONUtil
- JsonUtil工具类
- Json工具类 ---JsonUtil
- Json工具类JsonUtil
- Java-JsonUtil工具类
- JSONUtil工具类
- JSONUtil工具类
- jsonutil工具类
- Json转换工具类JsonUtil
- JsonUtil工具类的封装
- 常用JSON工具类JsonUtil封装
- json 转换工具jsonutil
- 自己封装Jackson的工具类——JsonUtil
- JsonUtil
- JsonUtil
- JsonUtil
- JSONUtil
- Linux内核裁剪的具体过程和方法
- 【Scikit-Learn 中文文档】多类和多标签算法
- 胶囊间的动态路由(2)
- Appium的安装与使用
- JAVASE之String
- 工具类分享之《JsonUtil》
- es6基础笔记整理
- 654. Maximum Binary Tree
- linux中readl()和writel()函数
- 【Scikit-Learn 中文文档】特征选择
- 响应式双飞翼雪碧图居中方法
- 关于将数组转为list的方法Arrays.asList(arr)的缺陷
- OIer JCY
- 解决问题:编译Latex遇到 file ended while scanning use of \@xdblarg or \@frac