当前位置:网站首页>【Utils】JsonUtil
【Utils】JsonUtil
2022-07-28 14:10:00 【One duck, one duck】
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
/** * Customized response structure of Tesco mall */
public class JsonUtils {
// Definition jackson object
private static final ObjectMapper MAPPER = new ObjectMapper();
/** * Converts an object to json character string . * <p>Title: pojoToJson</p> * <p>Description: </p> * @param data * @return */
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/** * take json Result set converted to object * * @param jsonData json data * @param beanType Object object type * @return */
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** * take json Data to pojo object list * <p>Title: jsonToList</p> * <p>Description: </p> * @param jsonData * @param beanType * @return */
public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** * hold json Convert to Map Tool method * @param jsonData * @param keyType * @param valueType * @param <K> * @param <V> * @return */
public static <K,V> Map<K,V> jsonToMap(String jsonData, Class<K> keyType,Class<V> valueType){
JavaType javaType = MAPPER.getTypeFactory().constructMapType(Map.class,keyType,valueType);
try {
Map<K,V> map = MAPPER.readValue(jsonData,javaType);
return map;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
边栏推荐
- 了解BFC特性,轻松实现自适应布局
- 第六章 支持向量机
- The strongest distributed locking tool: redisson
- 7. Dependency injection
- 关于栈的理解以及实际应用场景
- 多级缓存方案
- RSA用私钥加密数据公钥解密数据(不是签名验证过程)
- 掌握闭包,夯实基本功
- R language uses LM function to build multiple linear regression model, writes regression equation according to model coefficient, and uses conflict function to give 95% confidence interval of regressi
- Vite configuring path aliases in the project
猜你喜欢

30 day question brushing plan (II)

【LVGL事件(Events)】事件在不同组件上的应用(一)

Redis sentinel mechanism

RSA用私钥加密数据公钥解密数据(不是签名验证过程)

LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树

What is a spin lock? A spin lock means that when a thread attempts to acquire a lock, if the lock has been occupied by other threads, it will always cycle to detect whether the lock has been released,

Clickhouse分布式集群搭建

Security assurance is based on software life cycle -istio authentication mechanism
![[lvgl events] Application of events on different components (I)](/img/a8/7c24e68f3506bbef3c2e922729471c.png)
[lvgl events] Application of events on different components (I)

SLAM论文合集
随机推荐
安全保障基于软件全生命周期-PSP应用
TS literacy method - Basic chapter
webSocket聊天
Clickhouse架构与设计
qml 图片预览
安全保障基于软件全生命周期-NetworkPolicy应用
LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
.net for subtraction, intersection and union of complex type sets
Long closed period private placement products reappearance industry insiders have different views
Poj1860 currency exchange solution
Socket类关于TCP字符流编程的理解学习
Jmeter安装教程及登录增加token
Algorithm --- different paths (kotlin)
30 day question brushing training (I)
Record a fake login of cookie
【Utils】CookieUtil
Discrete logarithm problem (DLP) & Diffie Hellman problem (DHP)
创建线程池的四种方式
Understand BFC features and easily realize adaptive layout
DXF读写:标注样式组码中文说明