当前位置:网站首页>Correct conversion between JSON data and list collection
Correct conversion between JSON data and list collection
2022-06-28 09:48:00 【Autonomous watermelon L】
1.JSON Data and List Correct conversion between sets
@Test
public void testJsonObjectCastToList() {
// 0. Collection test data preparation
List<PersonInfo> personInfos = new ArrayList<>();
PersonInfo personInfoOne =
new PersonInfo(" Xiao Ming ", 18, 178.89);
PersonInfo personInfoTwo =
new PersonInfo(" Xiaohong ", 18, 165.56);
personInfos.add(personInfoOne);
personInfos.add(personInfoTwo);
// 1.JSON Data and List Correct conversion between sets
// 1.1 take List Set to JSON Data string
String str = JSON.toJSONString(personInfos);
System.out.println(str);
// [{"age":18,"height":178.89,"name":" Xiao Ming "},{"age":18,"height":165.56,"name":" Xiaohong "}]
// 1.2 take JSON The data goes to list aggregate
List<PersonInfo> personInfoList;
try {
personInfoList = JSON.parseArray(str, PersonInfo.class);
} catch (Exception e) {
log.error("JSON Parsing exceptions :{}", ExceptionUtils.getStackTrace(e));
personInfoList = new ArrayList<>();
}
System.out.println(personInfoList);
// [PersonInfo(name= Xiao Ming , age=18, height=178.89), PersonInfo(name= Xiaohong , age=18, height=165.56)]
/**
* 2. When the following code is executed , Abnormal transactions :
* com.alibaba.fastjson.JSONException: syntax error, expect {,
* actual error, pos 1, fastjson-version 1.2.62
*/
// // 2.1 Convert a set directly to a string
// String strMethod = String.valueOf(personInfos);
// System.out.println(strMethod);
// [PersonInfo(name= Xiao Ming , age=18, height=178.89), PersonInfo(name= Xiaohong , age=18, height=165.56)]
// // 2.2 take JSON The data goes to list aggregate
// List<PersonInfo> personInfoListStr = JSON.parseArray(strMethod, PersonInfo.class);
// System.out.println(personInfoListStr);
}边栏推荐
猜你喜欢

Unity AssetBundle资源打包与资源加载
![QT signal and slot communication mechanism (when multiple windows communicate back and forth [parent and child windows])](/img/17/57ffb7393b71eddc5ac92ae3944338.jpg)
QT signal and slot communication mechanism (when multiple windows communicate back and forth [parent and child windows])

虚拟机14安装win7(图教程)

我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!

用 Compose 实现个空调,为你的夏日带去清凉

Dbeaver connects to kingbasees V8 (ultra detailed graphic tutorial)

布隆过滤器 课程研究报告

HDI的盲孔设计,你注意到这个细节了吗?

How to reduce the risk of project communication?

PyGame game: "Changsha version" millionaire started, dare you ask? (multiple game source codes attached)
随机推荐
TCP实战案例之即时通信、BS架构模拟
布隆过滤器 课程研究报告
MySQL基础知识点总结
Unity loads AssetBundle resources from the server and writes them to local memory, and loads the downloaded and saved AB resources from local memory to the scene
函数的分文件编写
[happy Lantern Festival] guessing lantern riddles eating lantern festival full of vitality ~ (with lantern riddle guessing games)
2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠
Ingersoll Rand panel maintenance IR Ingersoll Rand microcomputer controller maintenance xe-145m
A strange execution plan. One table in the association query is not associated with any other tables
PMP考试重点总结六——图表整理
Numpy array: join, flatten, and add dimensions
Write a simple timeline
Sword finger offer | Fibonacci sequence
The concept of "tree structure" perfectly interprets the primary and secondary of things
Summary of PMP learning experience
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
Restful style
Dbeaver installation and use tutorial (super detailed installation and use tutorial)
Check whether the table contains rows SQL Server 2005 - check whether a table contains rows or not SQL Server 2005
bye! IE browser, this route edge continues to go on for IE