当前位置:网站首页>JSON数据与List集合之间的正确转换
JSON数据与List集合之间的正确转换
2022-06-28 09:25:00 【自律的西瓜L】
1.JSON数据与List集合之间的正确转换
@Test
public void testJsonObjectCastToList() {
// 0.集合测试数据准备
List<PersonInfo> personInfos = new ArrayList<>();
PersonInfo personInfoOne =
new PersonInfo("小明", 18, 178.89);
PersonInfo personInfoTwo =
new PersonInfo("小红", 18, 165.56);
personInfos.add(personInfoOne);
personInfos.add(personInfoTwo);
// 1.JSON数据与List集合之间的正确转换
// 1.1 将List集合转为JSON数据字符串
String str = JSON.toJSONString(personInfos);
System.out.println(str);
// [{"age":18,"height":178.89,"name":"小明"},{"age":18,"height":165.56,"name":"小红"}]
// 1.2 将JSON数据转为list集合
List<PersonInfo> personInfoList;
try {
personInfoList = JSON.parseArray(str, PersonInfo.class);
} catch (Exception e) {
log.error("JSON解析异常:{}", ExceptionUtils.getStackTrace(e));
personInfoList = new ArrayList<>();
}
System.out.println(personInfoList);
// [PersonInfo(name=小明, age=18, height=178.89), PersonInfo(name=小红, age=18, height=165.56)]
/**
* 2.以下代码执行时,会报异常:
* com.alibaba.fastjson.JSONException: syntax error, expect {,
* actual error, pos 1, fastjson-version 1.2.62
*/
// // 2.1 将集合直接转为字符串
// String strMethod = String.valueOf(personInfos);
// System.out.println(strMethod);
// [PersonInfo(name=小明, age=18, height=178.89), PersonInfo(name=小红, age=18, height=165.56)]
// // 2.2 将JSON数据转为list集合
// List<PersonInfo> personInfoListStr = JSON.parseArray(strMethod, PersonInfo.class);
// System.out.println(personInfoListStr);
}边栏推荐
- JDBC connection database (MySQL) steps
- 异常
- Valentine's Day - VBS learning (sentences, love words)
- Importerror: no module named image [duplicate] - importerror: no module named image [duplicate]
- The private attribute of this class can be used directly? New() in use!!!
- Fastjason filter field
- Dolphin scheduler uses system time
- JVM系列(2)——垃圾回收
- Data visualization makes correlation analysis easier to use
- Calcul des frais d'achat et de vente d'actions
猜你喜欢

Apache Doris 成为 Apache 顶级项目

HDI blind hole design, have you noticed this detail?

SQL optimization experience: from 30248 seconds to 0.001 seconds
![1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation](/img/1a/162b060a6498e58278b6ca50e4953c.png)
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation

1182:合影效果

函数的分文件编写

Prototype chain JS
![1180:分数线划定/P1068 [NOIP2009 普及组] 分数线划定](/img/1a/162b060a6498e58278b6ca50e4953c.png)
1180:分数线划定/P1068 [NOIP2009 普及组] 分数线划定

new URL(“www.jjj.com“)

Apache Doris becomes the top project of Apache
随机推荐
结巴分词器_分词器原理
Dolphin scheduler uses system time
PMP Exam key summary VI - chart arrangement
flink cep 跳过策略 AfterMatchSkipStrategy.skipPastLastEvent() 匹配过的不再匹配 碧坑指南
104. maximum depth of binary tree
1181: integer parity sort
函数的分文件编写
Understanding the IO model
在本类私有属性直接使用?new()在使用!!!
redis5.0的槽点迁移,随意玩(单机迁移集群)
股票 停牌
==和eqauls()的区别
Use of Jasper soft studio report tool and solution of thorny problems
Play SFTP upload file
Do static code blocks always execute first? The pattern is smaller!!!
A strange execution plan. One table in the association query is not associated with any other tables
Data modeling based on wide table
多线程-并发并行-线程进程
自动转换之-面试题
2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠