jaskSon二次封装,使用更方便
2016-02-18 16:52
363 查看
原帖地址找不到了,抱歉,如有原作者要求我即可删除。。。
public class JacksonUtil { static ObjectMapper mapper; /** * ObjectMapper 配置及初始化 */ public static ObjectMapper Instance() { if (mapper == null) { mapper = new ObjectMapper(); mapper.getSerializationConfig().withDateFormat(new SimpleDateFormat("yyyy-MM-dd")); mapper.getDeserializationConfig().withDateFormat(new SimpleDateFormat("yyyy-MM-dd")); mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false); mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) ; mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize( Object value, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException { jg.writeString(""); } }); } return mapper; } /** * 将原始Json转为T类型实体 */ public static <T> T ToEntity(String content, Class<T> classT) { T result = null; try { result = Instance().readValue(content, classT); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 将原始Json转为T类型实体集合 */ public static <T> List<T> ToEntityS(String content, Class<T> classT) { List<T> result = null; try { JavaType javaType = Instance().getTypeFactory().constructParametricType(List.class, classT); result = Instance().readValue(content, javaType); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 遍历到指定节点并将节点中内容转为T类型实体 */ public static <T> T ToEntityByNode(String content, Class<T> classT, String... path) { T result = null; try { result = Instance().readValue(GetNodeString(content, path), classT); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 遍历到指定节点并将节点中内容转为T类型实体集合 */ public static <T> List<T> ToEntitySByNode(String content, Class<T> classT, String... path) { List<T> result = null; try { JavaType javaType = Instance().getTypeFactory().constructParametricType(List.class, classT); result = Instance().readValue(GetNodeString(content, path), javaType); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 获取指定节点的原始值 * key,value(String) */ public static String GetNodeStringValue(String content, String... path) { String result = null; try { JsonNode jsonNode = Instance().readTree(content); for (String p : path) { jsonNode = jsonNode.path(p); } result = jsonNode.getTextValue(); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 获取指定节点boolean值 * key,value(String) */ public static boolean GetNodeBooleanValue(String content, String... path) { boolean result = false; try { JsonNode jsonNode = Instance().readTree(content); for (String p : path) { jsonNode = jsonNode.path(p); } result = jsonNode.getBooleanValue(); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 获取指定节点的原始值 * key,value(Int) */ public static int GetNodeIntValue(String content, String... path) { int result = 0; try { JsonNode jsonNode = Instance().readTree(content); for (String p : path) { jsonNode = jsonNode.path(p); } result = jsonNode.getIntValue(); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 以字符串形式获取指定节点中的内容(*与数值不同,节点中内容可能为 Json Array 等,并非原始值) */ public static String GetNodeString(String content, String... path) { String result = null; try { JsonNode jsonNode = Instance().readTree(content); for (String p : path) { jsonNode = jsonNode.path(p); } result = jsonNode + ""; } catch (IOException e) { e.printStackTrace(); } return result; } /** * Object 转为 Json */ public static String ToJSon(Object obj) { String result = null; try { result = Instance().writeValueAsString(obj); } catch (IOException e) { e.printStackTrace(); } return result; } }
相关文章推荐
- 二分查找算法
- [转]9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
- 使用Jsoup抓取数据
- wdcp安装php zip扩展
- linux中select()函数分析
- HBASE预先分配regions的实现
- 如何使用vs2012的performance analysis tool 性能分析工具的使用
- mysql5.6子查询的优化
- Java IO最详解
- JavaScriptCore详解
- 深入理解Linux守护进程
- struts1重定向 浅谈ActionForward的三种重定向
- Java垃圾回收机制
- python实现IP地址更改
- nginx的特点
- JSON认识
- Windows live writer 2012 测试
- SQL查询表中的有那些索引
- KMP 最短循环节点长度证明
- USACO 4.2.1 网络流 最高标号预流推进(HLPP)