当前位置:网站首页>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 ------ 14 ------ gotest
- JS regular case-
- VMware virtual machine configuration static IP
- 华为S5700交换机初始化和配置telnet,ssh用户方法
- How to configure GDAL under idea
- freetype库的移植
- Quelle est la définition? Qu'est - ce qu'une déclaration? Quelle est la différence?
- Usage of (case, when) in PostgreSQL
- vcs import src < ros2. Repos failed
- yarn link 是如何帮助开发者对 NPM 包进行 debug 的?
猜你喜欢
Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire
Redis batch startup and shutdown script
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
Free use until 2015 -- viz artist multi touch plug-in package
vcs import src < ros2. Repos failed
Go language foundation ----- 03 ----- process control, function, value transfer, reference transfer, defer function
Ventuz Foundation Series "one step at the door"
*p++、*++p、++*p、(*p)++
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
My touch screen production "brief history" 2
随机推荐
【cocos creator】获取资源uuid
IP production stream is so close to me
When did you find out that youth was over
多旅行商问题——公式和求解过程概述
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
Huawei s5700 switch initialization and configuration Telnet, SSH user methods
PDO and SDO concepts
Clip Related Script
Go language - loop statement
[step on the pit series] MySQL failed to modify the root password
[MySQL 12] MySQL 8.0.18 reinitialization
regular expression
The general trend of data news releases the power of visual reporting ----- essays after reading
Go language foundation ----- 16 ----- goroutine, GPM model
Huawei switches are configured with SSH login remote management switches
C2-关于VCF文件合并的几种方法
Go language foundation ----- 08 ----- interface
什麼是定義?什麼是聲明?它們有何區別?
haproxy+keepalived搭建01
Worldview satellite remote sensing image data / meter resolution remote sensing image