1. package org.linlinjava.litemall.core.util;
  2.  
  3. import com.fasterxml.jackson.core.type.TypeReference;
  4. import com.fasterxml.jackson.databind.JsonNode;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import org.apache.commons.logging.Log;
  7. import org.apache.commons.logging.LogFactory;
  8.  
  9. import java.io.IOException;
  10. import java.util.List;
  11. import java.util.Map;
  12.  
  13. public class JacksonUtil {
  14.  
  15. private static final Log logger = LogFactory.getLog(JacksonUtil.class);
  16.  
  17. public static String parseString(String body, String field) {
  18. ObjectMapper mapper = new ObjectMapper();
  19. JsonNode node;
  20. try {
  21. node = mapper.readTree(body);
  22. JsonNode leaf = node.get(field);
  23. if (leaf != null)
  24. return leaf.asText();
  25. } catch (IOException e) {
  26. logger.error(e.getMessage(), e);
  27. }
  28. return null;
  29. }
  30.  
  31. public static List<String> parseStringList(String body, String field) {
  32. ObjectMapper mapper = new ObjectMapper();
  33. JsonNode node;
  34. try {
  35. node = mapper.readTree(body);
  36. JsonNode leaf = node.get(field);
  37.  
  38. if (leaf != null)
  39. return mapper.convertValue(leaf, new TypeReference<List<String>>() {
  40. });
  41. } catch (IOException e) {
  42. logger.error(e.getMessage(), e);
  43. }
  44. return null;
  45. }
  46.  
  47. public static Integer parseInteger(String body, String field) {
  48. ObjectMapper mapper = new ObjectMapper();
  49. JsonNode node;
  50. try {
  51. node = mapper.readTree(body);
  52. JsonNode leaf = node.get(field);
  53. if (leaf != null)
  54. return leaf.asInt();
  55. } catch (IOException e) {
  56. logger.error(e.getMessage(), e);
  57. }
  58. return null;
  59. }
  60.  
  61. public static List<Integer> parseIntegerList(String body, String field) {
  62. ObjectMapper mapper = new ObjectMapper();
  63. JsonNode node;
  64. try {
  65. node = mapper.readTree(body);
  66. JsonNode leaf = node.get(field);
  67.  
  68. if (leaf != null)
  69. return mapper.convertValue(leaf, new TypeReference<List<Integer>>() {
  70. });
  71. } catch (IOException e) {
  72. logger.error(e.getMessage(), e);
  73. }
  74. return null;
  75. }
  76.  
  77. public static Boolean parseBoolean(String body, String field) {
  78. ObjectMapper mapper = new ObjectMapper();
  79. JsonNode node;
  80. try {
  81. node = mapper.readTree(body);
  82. JsonNode leaf = node.get(field);
  83. if (leaf != null)
  84. return leaf.asBoolean();
  85. } catch (IOException e) {
  86. logger.error(e.getMessage(), e);
  87. }
  88. return null;
  89. }
  90.  
  91. public static Short parseShort(String body, String field) {
  92. ObjectMapper mapper = new ObjectMapper();
  93. JsonNode node;
  94. try {
  95. node = mapper.readTree(body);
  96. JsonNode leaf = node.get(field);
  97. if (leaf != null) {
  98. Integer value = leaf.asInt();
  99. return value.shortValue();
  100. }
  101. } catch (IOException e) {
  102. logger.error(e.getMessage(), e);
  103. }
  104. return null;
  105. }
  106.  
  107. public static Byte parseByte(String body, String field) {
  108. ObjectMapper mapper = new ObjectMapper();
  109. JsonNode node;
  110. try {
  111. node = mapper.readTree(body);
  112. JsonNode leaf = node.get(field);
  113. if (leaf != null) {
  114. Integer value = leaf.asInt();
  115. return value.byteValue();
  116. }
  117. } catch (IOException e) {
  118. logger.error(e.getMessage(), e);
  119. }
  120. return null;
  121. }
  122.  
  123. public static <T> T parseObject(String body, String field, Class<T> clazz) {
  124. ObjectMapper mapper = new ObjectMapper();
  125. JsonNode node;
  126. try {
  127. node = mapper.readTree(body);
  128. node = node.get(field);
  129. return mapper.treeToValue(node, clazz);
  130. } catch (IOException e) {
  131. logger.error(e.getMessage(), e);
  132. }
  133. return null;
  134. }
  135.  
  136. public static Object toNode(String json) {
  137. if (json == null) {
  138. return null;
  139. }
  140. ObjectMapper mapper = new ObjectMapper();
  141. try {
  142.  
  143. return mapper.readTree(json);
  144. } catch (IOException e) {
  145. logger.error(e.getMessage(), e);
  146. }
  147.  
  148. return null;
  149. }
  150.  
  151. public static Map<String, String> toMap(String data) {
  152. ObjectMapper objectMapper = new ObjectMapper();
  153. try {
  154. return objectMapper.readValue(data, new TypeReference<Map<String, String>>() {
  155. });
  156. } catch (IOException e) {
  157. logger.error(e.getMessage(), e);
  158. }
  159. return null;
  160. }
  161.  
  162. }

JacksonUtil的更多相关文章

  1. 第一章 JacksonUtil 序列化与反序列化属性总结

    1.json-lib与Jackson 关于json-lib与Jackson对比总结如下: 1).性能方面,Jackson的处理能力高出Json-lib10倍左右. 2).json-lib已经停止更新, ...

  2. Jackson 对象与json数据互转工具类JacksonUtil

    1,User对象 package com.st.json; import java.util.Date; /** * @Description: JSON序列化和反序列化使用的User类 * @aut ...

  3. springMVC、httpClient调用别人提供的接口!!!(外加定时调用)

    import com.ibm.db.util.AppConfig; import com.ibm.db.util.JacksonUitl; import org.apache.http.HttpEnt ...

  4. Android 通过Base64上传图片到服务器

    之前做上传图片是采用HttpServlet上传,不过用了一下Base64上传图片后,感觉比HttpServlet方便很多,大家也可以跟着尝试一下. 前台图片处理:(传Bitmap对象即可) /** * ...

  5. PowerMockito(PowerMock用法)

    网络上大部分是powermock 的用法, PowerMock有两个重要的注解: –@RunWith(PowerMockRunner.class) –@PrepareForTest( { YourCl ...

  6. 介绍4款json的java类库 及 其性能测试

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...

  7. Maven搭建SpringMVC+MyBatis+Json项目(多模块项目)

    一.开发环境 Eclipse:eclipse-jee-luna-SR1a-win32; JDK:jdk-8u121-windows-i586.exe; MySql:MySQL Server 5.5; ...

  8. SpringAop注解实现日志的存储

    一.介绍 1.AOP的作用 在OOP中,正是这种分散在各处且与对象核心功能无关的代码(横切代码)的存在,使得模块复用难度增加.AOP则将封装好的对象剖开,找出其中对多个对象产生影响的公共行为,并将其封 ...

  9. Java几种常用JSON库性能比较

    本篇通过JMH来测试一下Java中几种常见的JSON解析库的性能. 每次都在网上看到别人说什么某某库性能是如何如何的好,碾压其他的库.但是百闻不如一见,只有自己亲手测试过的才是最值得相信的. JSON ...

随机推荐

  1. Linux学习-第二章(命令)20200216

  2. Ubuntu下caffe:用自己的图片训练并测试AlexNet模型

    参考博客:https://blog.csdn.net/eereere/article/details/79118645#commentBox 目录 1.准备图片 2. 将 图片路径写入txt 参考 这 ...

  3. Centos6.5 安装zabbix3(收藏,非原创)

    1.安装PHP Zabbix 3.0对PHP的要求最低为5.4,而CentOS6默认为5.3.3,完全不满足要求,故需要利用第三方源,将PHP升级到5.4以上,注意,不支持PHP7 rpm -ivh  ...

  4. Springboot过滤器注解简笔

    对多个过滤的注解      @WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.js ...

  5. 洛谷 P1709 隐藏口令

    题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<=5,000,000),然后他把S顺时针绕成一个圈,每次取一个做开头字母并 ...

  6. Windbg 实践之符号篇

    How to display the size value 1)一开始不会加载,chksym 了一下就加载了. 2) 新版本已经可以显示size的大小了 3)?? 显示变量的类型 4)x std::v ...

  7. Aras Innovator Method集成Visual Studio

    首先下载集成安装包: https://github.com/RazorleafLabs/Aras-Integration-to-Visual-Studio 解压文件包,找到Aras-Integrati ...

  8. 新浪SAE云平台下使用codeigniter的数据库配置

    新浪SAE云平台下使用codeigniter的数据库配置 投稿:shichen2014 字体:[增加 减小] 类型:转载 这篇文章主要介绍了新浪SAE云平台下使用codeigniter的数据库配置,主 ...

  9. OpenMP笔记(二)

    原文:https://www.bearoom.xyz/2019/02/18/openmp2/ OpenMP是由三部分组成的:指令.库函数和环境变量. 一.指令 在C/C++中使用OpenMP需要用到的 ...

  10. 35. docker swarm dockerStack 部署 投票应用

    1. 编写 docker-compose.yml # docker-compose.yml version: "3" services: redis: image: redis:a ...