当前位置:网站首页>【Utils】JsonUtil
【Utils】JsonUtil
2022-07-28 13:12:00 【一鸭一鸭唷】
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;
/** * 易购商城自定义响应结构 */
public class JsonUtils {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/** * 将对象转换成json字符串。 * <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;
}
/** * 将json结果集转化为对象 * * @param jsonData json数据 * @param beanType 对象中的object类型 * @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;
}
/** * 将json数据转换成pojo对象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;
}
/** * 把json转换为Map工具方法 * @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;
}
}
边栏推荐
- Thoroughly master binary search
- [Architecture] reading notes of three micro service books with high scores
- How to play a data mining game entry Edition
- 第六章 支持向量机
- Multithreading and high concurrency (III) -- source code analysis AQS principle
- word打字时后面的字会消失是什么原因?如何解决?
- R language uses dpois function to generate Poisson distribution density data and plot function to visualize Poisson distribution density data
- [security] read rfc6749 and understand the authorization code mode under oauth2.0
- Slam thesis collection
- 多级缓存方案
猜你喜欢
JWT login authentication + token automatic renewal scheme, well written!

修订版 | 目标检测:速度和准确性比较(Faster R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)...
![[basic course of flight control development 7] crazy shell · open source formation UAV SPI (barometer data acquisition)](/img/ad/e0bc488c238a260768f7e7faec87d0.png)
[basic course of flight control development 7] crazy shell · open source formation UAV SPI (barometer data acquisition)

Record a fake login of cookie

Security assurance is based on software life cycle -psp application

多线程与高并发(三)—— 源码解析 AQS 原理

DXF reading and writing: align the calculation of the position of the dimension text in the middle and above
![[lvgl events] Application of events on different components (I)](/img/a8/7c24e68f3506bbef3c2e922729471c.png)
[lvgl events] Application of events on different components (I)

Istio四之故障注入和链路追踪

《机器学习》(周志华) 第6章 支持向量 学习心得 笔记
随机推荐
第六章 支持向量机
ES6 what amazing writing methods have you used
关于栈的理解以及实际应用场景
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置draw_quantiles参数添加指定分位数横线(例如,50%分位数、中位数)
TS扫盲大法-基础篇
R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize violin diagrams, set the palette parameter, and customize the border colors of violin diagrams at different l
修订版 | 目标检测:速度和准确性比较(Faster R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)...
30 day question brushing plan (IV)
The domestic API management tool eolink is very easy to use, creating an efficient research and development tool
R语言因子数据的表格和列联表(交叉表)生成:使用summay函数分析列表查看卡方检验结果判断两个因子变量是否独立(使用卡方检验验证独立性)
Security assurance is based on software life cycle - networkpolicy application
对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解
Tutorial on the principle and application of database system (059) -- MySQL exercise questions: operation questions 1-10 (III)
Rolling update strategy of deployment.
Operator3 - design an operator
Product Manager: job responsibility table
解决跨越的几种方案
Uva11175 digraph D and E from D to e and back
Security assurance is based on software life cycle -psp application
Clickhouse架构与设计