当前位置:网站首页>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;
}
边栏推荐
- Go language foundation ----- 08 ----- interface
- 华为交换机配置ssh登录远程管理交换机
- Go language foundation ------ 12 ------ JSON
- experiment.........
- Go language foundation ----- 19 ----- context usage principle, interface, derived context (the multiplexing of select can be better understood here)
- [cocos creator] Click the button to switch the interface
- [untitled]
- What is a data type? What is the use of data types?
- s7700设备如何清除console密码
- vcs import src < ros2. Repos failed
猜你喜欢

Iterm2设置

Go language foundation ------ 14 ------ gotest

What to do after the browser enters the URL

一篇文章让你读懂-曼彻斯特编码

Professor Zhang Yang of the University of Michigan is employed as a visiting professor of Shanghai Jiaotong University, China (picture)

Docker installs MySQL and successfully uses Navicat connection

PostGIS space function

WPF:解决MaterialDesign:DialogHost 无法关闭问题

Go language foundation ----- 05 ----- structure

C language learning notes (mind map)
随机推荐
PHP common sorting algorithm
方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!
Go language foundation ----- 02 ----- basic data types and operators
P2622 关灯问题II(状态压缩 搜索)
【LeetCode】2. Valid parentheses · valid parentheses
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
在浏览器输入url后执行什么
JS regular case-
Go language foundation ----- 16 ----- goroutine, GPM model
Enter three times and guess a number
Professor Zhang Yang of the University of Michigan is employed as a visiting professor of Shanghai Jiaotong University, China (picture)
jsutlis
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
Go language foundation ----- 13 ----- file
Huawei s5700 switch initialization and configuration Telnet, SSH user methods
[untitled]
Go language foundation ----- 03 ----- process control, function, value transfer, reference transfer, defer function
PostGIS space function
Go language foundation ----- 19 ----- context usage principle, interface, derived context (the multiplexing of select can be better understood here)
Huawei s5700 switch initialization and configuration SSH and telnet remote login methods