JacksonUtil
- package org.linlinjava.litemall.core.util;
- import com.fasterxml.jackson.core.type.TypeReference;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import java.io.IOException;
- import java.util.List;
- import java.util.Map;
- public class JacksonUtil {
- private static final Log logger = LogFactory.getLog(JacksonUtil.class);
- public static String parseString(String body, String field) {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode node;
- try {
- node = mapper.readTree(body);
- JsonNode leaf = node.get(field);
- if (leaf != null)
- return leaf.asText();
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- public static List<String> parseStringList(String body, String field) {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode node;
- try {
- node = mapper.readTree(body);
- JsonNode leaf = node.get(field);
- if (leaf != null)
- return mapper.convertValue(leaf, new TypeReference<List<String>>() {
- });
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- public static Integer parseInteger(String body, String field) {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode node;
- try {
- node = mapper.readTree(body);
- JsonNode leaf = node.get(field);
- if (leaf != null)
- return leaf.asInt();
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- public static List<Integer> parseIntegerList(String body, String field) {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode node;
- try {
- node = mapper.readTree(body);
- JsonNode leaf = node.get(field);
- if (leaf != null)
- return mapper.convertValue(leaf, new TypeReference<List<Integer>>() {
- });
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- public static Boolean parseBoolean(String body, String field) {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode node;
- try {
- node = mapper.readTree(body);
- JsonNode leaf = node.get(field);
- if (leaf != null)
- return leaf.asBoolean();
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- public static Short parseShort(String body, String field) {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode node;
- try {
- node = mapper.readTree(body);
- JsonNode leaf = node.get(field);
- if (leaf != null) {
- Integer value = leaf.asInt();
- return value.shortValue();
- }
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- public static Byte parseByte(String body, String field) {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode node;
- try {
- node = mapper.readTree(body);
- JsonNode leaf = node.get(field);
- if (leaf != null) {
- Integer value = leaf.asInt();
- return value.byteValue();
- }
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- public static <T> T parseObject(String body, String field, Class<T> clazz) {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode node;
- try {
- node = mapper.readTree(body);
- node = node.get(field);
- return mapper.treeToValue(node, clazz);
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- public static Object toNode(String json) {
- if (json == null) {
- return null;
- }
- ObjectMapper mapper = new ObjectMapper();
- try {
- return mapper.readTree(json);
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- public static Map<String, String> toMap(String data) {
- ObjectMapper objectMapper = new ObjectMapper();
- try {
- return objectMapper.readValue(data, new TypeReference<Map<String, String>>() {
- });
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- return null;
- }
- }
JacksonUtil的更多相关文章
- 第一章 JacksonUtil 序列化与反序列化属性总结
1.json-lib与Jackson 关于json-lib与Jackson对比总结如下: 1).性能方面,Jackson的处理能力高出Json-lib10倍左右. 2).json-lib已经停止更新, ...
- Jackson 对象与json数据互转工具类JacksonUtil
1,User对象 package com.st.json; import java.util.Date; /** * @Description: JSON序列化和反序列化使用的User类 * @aut ...
- springMVC、httpClient调用别人提供的接口!!!(外加定时调用)
import com.ibm.db.util.AppConfig; import com.ibm.db.util.JacksonUitl; import org.apache.http.HttpEnt ...
- Android 通过Base64上传图片到服务器
之前做上传图片是采用HttpServlet上传,不过用了一下Base64上传图片后,感觉比HttpServlet方便很多,大家也可以跟着尝试一下. 前台图片处理:(传Bitmap对象即可) /** * ...
- PowerMockito(PowerMock用法)
网络上大部分是powermock 的用法, PowerMock有两个重要的注解: –@RunWith(PowerMockRunner.class) –@PrepareForTest( { YourCl ...
- 介绍4款json的java类库 及 其性能测试
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...
- Maven搭建SpringMVC+MyBatis+Json项目(多模块项目)
一.开发环境 Eclipse:eclipse-jee-luna-SR1a-win32; JDK:jdk-8u121-windows-i586.exe; MySql:MySQL Server 5.5; ...
- SpringAop注解实现日志的存储
一.介绍 1.AOP的作用 在OOP中,正是这种分散在各处且与对象核心功能无关的代码(横切代码)的存在,使得模块复用难度增加.AOP则将封装好的对象剖开,找出其中对多个对象产生影响的公共行为,并将其封 ...
- Java几种常用JSON库性能比较
本篇通过JMH来测试一下Java中几种常见的JSON解析库的性能. 每次都在网上看到别人说什么某某库性能是如何如何的好,碾压其他的库.但是百闻不如一见,只有自己亲手测试过的才是最值得相信的. JSON ...
随机推荐
- Linux学习-第二章(命令)20200216
- Ubuntu下caffe:用自己的图片训练并测试AlexNet模型
参考博客:https://blog.csdn.net/eereere/article/details/79118645#commentBox 目录 1.准备图片 2. 将 图片路径写入txt 参考 这 ...
- Centos6.5 安装zabbix3(收藏,非原创)
1.安装PHP Zabbix 3.0对PHP的要求最低为5.4,而CentOS6默认为5.3.3,完全不满足要求,故需要利用第三方源,将PHP升级到5.4以上,注意,不支持PHP7 rpm -ivh ...
- Springboot过滤器注解简笔
对多个过滤的注解 @WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.js ...
- 洛谷 P1709 隐藏口令
题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<=5,000,000),然后他把S顺时针绕成一个圈,每次取一个做开头字母并 ...
- Windbg 实践之符号篇
How to display the size value 1)一开始不会加载,chksym 了一下就加载了. 2) 新版本已经可以显示size的大小了 3)?? 显示变量的类型 4)x std::v ...
- Aras Innovator Method集成Visual Studio
首先下载集成安装包: https://github.com/RazorleafLabs/Aras-Integration-to-Visual-Studio 解压文件包,找到Aras-Integrati ...
- 新浪SAE云平台下使用codeigniter的数据库配置
新浪SAE云平台下使用codeigniter的数据库配置 投稿:shichen2014 字体:[增加 减小] 类型:转载 这篇文章主要介绍了新浪SAE云平台下使用codeigniter的数据库配置,主 ...
- OpenMP笔记(二)
原文:https://www.bearoom.xyz/2019/02/18/openmp2/ OpenMP是由三部分组成的:指令.库函数和环境变量. 一.指令 在C/C++中使用OpenMP需要用到的 ...
- 35. docker swarm dockerStack 部署 投票应用
1. 编写 docker-compose.yml # docker-compose.yml version: "3" services: redis: image: redis:a ...