Json字符串转Java对象和List代码实例
作者:听风的dog 时间:2022-03-11 15:47:09
对象POJO和JSON互转
public class JsonUtil {
/**
* JSON 转 POJO
*/
public static <T> T getObject(String pojo, Class<T> tclass) {
try {
return JSONObject.parseObject(pojo, tclass);
} catch (Exception e) {
log.error(tclass + "转 JSON 失败");
}
return null;
}
/**
* POJO 转 JSON
*/
public static <T> String getJson(T tResponse){
String pojo = JSONObject.toJSONString(tResponse);
return pojo;
}
}
List集合和JSON互转工具类
public class JsonListUtil {
/**
* List<T> 转 json 保存到数据库
*/
public static <T> String listToJson(List<T> ts) {
String jsons = JSON.toJSONString(ts);
return jsons;
}
/**
* json 转 List<T>
*/
public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
@SuppressWarnings("unchecked")
List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
return ts;
}
}
来源:https://www.cnblogs.com/winddogg/p/10924487.html
标签:Json,字符串,转,Java,List
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java8 使用 stream().sorted()对List集合进行排序的操作
2022-11-23 20:57:11
![](https://img.aspxhome.com/file/2023/0/71930_0s.jpg)
java连接ElasticSearch集群操作
2023-11-28 04:06:24
Mybatis中的resultType和resultMap查询操作实例详解
2022-02-24 17:05:42
![](https://img.aspxhome.com/file/2023/3/76233_0s.png)
java解析excel文件的方法
2023-05-13 15:53:44
java中request常用方法小结
2022-09-13 06:19:45
![](https://img.aspxhome.com/file/2023/5/82995_0s.png)
Android使用Kotlin实现多节点进度条
2022-12-01 09:40:06
![](https://img.aspxhome.com/file/2023/7/87327_0s.jpg)
Struts 2中的constant配置详解
2023-11-10 08:18:18
Android三方依赖冲突Gradle中exclude的使用
2023-05-05 05:09:56
![](https://img.aspxhome.com/file/2023/5/87335_0s.png)
Java新特性之Nashorn_动力节点Java学院整理
2022-07-31 17:18:13
![](https://img.aspxhome.com/file/2023/2/62822_0s.png)
Springboot2.x+ShardingSphere实现分库分表的示例代码
2023-11-26 01:34:07
![](https://img.aspxhome.com/file/2023/1/59591_0s.jpg)
mybatis如何实现saveOrUpdate
2021-07-18 10:33:11
让Java后台MySQL数据库能够支持emoji表情的方法
2022-12-30 04:24:45
剑指Offer之Java算法习题精讲链表与字符串及数组
2022-10-03 19:10:17
![](https://img.aspxhome.com/file/2023/2/62732_0s.png)
mybatis-plus生成mapper扩展文件的方法
2023-08-10 07:29:05
![](https://img.aspxhome.com/file/2023/4/58044_0s.png)
Unity实现10天签到系统
2022-07-11 23:40:01
![](https://img.aspxhome.com/file/2023/0/89820_0s.jpg)
Spring SpringMVC在启动完成后执行方法源码解析
2023-01-01 12:46:57
Spring Boot conditional注解用法详解
2022-03-19 02:32:25
![](https://img.aspxhome.com/file/2023/5/61795_0s.png)
springboot 集成redission 以及分布式锁的使用详解
2023-06-20 06:48:43
![](https://img.aspxhome.com/file/2023/6/83486_0s.jpg)
Java中死锁与活锁的具体实现
2023-10-29 01:48:02
![](https://img.aspxhome.com/file/2023/4/80844_0s.jpg)
浅谈maven单元测试设置代理
2022-03-14 20:23:04