当前位置:网站首页>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);
}
边栏推荐
- A strange execution plan. One table in the association query is not associated with any other tables
- SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
- DolphinScheduler使用系统时间
- The digital human industry is about to break out. What is the market pattern?
- 代理模式(Proxy)
- How to view the web password saved by Google browser
- Differences between task parameter types inout and ref
- Dolphin scheduler uses system time
- Two interview demo
- bad zipfile offset (local header sig)
猜你喜欢
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
Matplotlib attribute and annotation
老板叫我写个APP自动化--Yaml文件读取--内附整个框架源码
Global exception handlers and unified return results
Data visualization makes correlation analysis easier to use
Decorator
A classic JVM class loaded interview question class singleton{static singleton instance = new singleton(); private singleton() {}
bye! IE browser, this route edge continues to go on for IE
The digital human industry is about to break out. What is the market pattern?
缓存之王Caffeine Cache,性能比Guava更强
随机推荐
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
Composite pattern
==和eqauls()的区别
bad zipfile offset (local header sig)
如图 用sql行转列 图一原表,图二希望转换后
Unity 从服务器加载AssetBundle资源写入本地内存,并将下载保存的AB资源从本地内存加载至场景
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
数字人行业爆发在即,市场格局几何?
布隆过滤器 课程研究报告
手机号、邮箱正则验证[通俗易懂]
全局异常处理器与统一返回结果
线程的生命周期
ffmpeg录音录像
[ybtoj advanced training guide] maximum separation [hash] [Floyd]
纵观jBPM从jBPM3到jBPM5以及Activiti
What is online account opening? Is it safe to open an account online now?
JVM family (2) - garbage collection
JSON数据与List集合之间的正确转换
通过PyTorch构建的LeNet-5网络对手写数字进行训练和识别
异常