当前位置:网站首页>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;
}
边栏推荐
- Ventuz Foundation Series "one step at the door"
- P1896 [SCOI2005] 互不侵犯(状压dp)
- How to configure GDAL under idea
- Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
- [USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
- Worldview satellite remote sensing image data / meter resolution remote sensing image
- tslib库的移植
- 华为S5700交换机初始化和配置SSH和TELNET远程登录方法
- Differences between tp3.2 and tp5.0
- Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
猜你喜欢
![[cocos creator] Click the button to switch the interface](/img/b8/f0fd54a2a197cbfd788990e2806b52.png)
[cocos creator] Click the button to switch the interface

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

Go language foundation ----- 03 ----- process control, function, value transfer, reference transfer, defer function

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

Pat grade a 1027 colors in Mars

Research shows that breast cancer cells are more likely to enter the blood when patients sleep

Pat class a 1030 travel plan

多旅行商问题——公式和求解过程概述

PostGIS space function

Go language foundation ----- 07 ----- method
随机推荐
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
超限黑客认知
JS regular case-
tp3.2和tp5.0的区别
P2622 关灯问题II(状态压缩 搜索)
Professor Zhang Yang of the University of Michigan is employed as a visiting professor of Shanghai Jiaotong University, China (picture)
2020-12-12
Pat class a 1028 list sorting
haproxy+keepalived集群搭建02
Redis batch startup and shutdown script
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
Go language foundation ----- 08 ----- interface
华为S5700交换机初始化和配置telnet,ssh用户方法
PHP wechat red packet grabbing algorithm
How does yarn link help developers debug NPM packages?
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
华为交换机Console密码重置、设备初始化、默认密码
oracle 插入单引号
【LeetCode】2. Valid parentheses · valid parentheses
Free use until 2015 -- viz artist multi touch plug-in package