当前位置:网站首页>JSON与Object之间转换
JSON与Object之间转换
2022-07-03 07:56:00 【八荒.】
在开发过程种;往往需要进行复杂数据的存储,我们一般使用json字符串的形式存储复杂数据,废话不多说上案例:**
使用net的JSON**
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
1:字符串json转化对象实体类对象或者集合
//对象集合转化json字符串
List<Test> allorderList = new ArrayList();
JSONArray exportDataJson = JSONArray.fromObject(allorderList);
String exportData = exportDataJson.toString();
//json字符串转化对象集合
JSONArray jsonArray = JSONArray.fromObject(json);
List<Test> reportVoList =JSONArray.toList(jsonArray, Test.class);
2:字符串json与 复杂集合的相应转化
/**
*
* List<Map<String, Object>>数据 转化json字符串存储数据
* @param json
* @return
*
*/
List<Map> list = new ArrayList();
Map map = new HashMap();
map.put("deptName",i.getDeptName());
List<CaterHistoryBusinessReportVO> lunchResult=new ArrayList();
List<CaterHistoryBusinessReportVO> dinnerResult=new ArrayList();
map.put("morningList",lunchResult);
map.put("dinnerList",dinnerResult);
list.add(map);
JSONArray jsonArray = JSONArray.fromObject(list);
String jsonResult = jsonArray.toString();
/**
*
* json信息转化 List<Map<String, Object>>数据
* @param json
* @return
*
*/‘
public static List<Map<String, Object>> toListMap(String json) {
List<Object> list = JSON.parseArray(json);
List<Map<String, Object>> listw = new ArrayList<Map<String, Object>>();
for (Object object : list) {
Map<String, Object> ret = (Map<String, Object>) object;
listw.add(ret);
}
return listw;
}
边栏推荐
- Getting started with minicom
- [cocos creator] Click the button to switch the interface
- GoLang之结构体
- 优质博客——
- Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire
- Worldview satellite remote sensing image data / meter resolution remote sensing image
- 超限黑客认知
- The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
- Redis查看客户端连接
- What is definition? What is a statement? What is the difference between them?
猜你喜欢
一条通往服务器所有端口的隧道
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
An article for you to understand - Manchester code
Technology dry goods | Roberta of the migration of mindspore NLP model - emotion analysis task
The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
Professor Zhang Yang of the University of Michigan is employed as a visiting professor of Shanghai Jiaotong University, China (picture)
How to configure GDAL under idea
Pat grade a 1029 median
[step on the pit series] MySQL failed to modify the root password
随机推荐
[at] ABC 258g - Triangle triples reachable - violence
Pat class a 1030 travel plan
Iterm2 setting
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
输入三次猜一个数字
Go language foundation ------ 14 ------ gotest
Harmonyos third training notes
Pat class a 1028 list sorting
Client server model
JS to implement publish and subscribe
Professor Zhang Yang of the University of Michigan is employed as a visiting professor of Shanghai Jiaotong University, China (picture)
一个实习生的CnosDB之旅
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
一篇文章让你读懂-曼彻斯特编码
Static keyword
WorldView卫星遥感影像数据/米级分辨率遥感影像
Redis batch startup and shutdown script
How does yarn link help developers debug NPM packages?
HDMI2.1与HDMI2.0的区别以及转换PD信号。
Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select