List<对象>转JSON字符串以及JSON字符串转List<对象>

embedded/2024/10/20 3:54:40/

签名:但行好事,莫问前程。

文章目录

  • 前言
  • 一、工作需求
  • 二、List<对象>转JSON字符串
  • 三、JSON字符串转List<对象>
  • 总结


前言

记录一下List<对象>转JSON字符串以及JSON字符串转List<对象>


一、工作需求

产品需求中有一个需求要求页面布局以JSON保存
在这里插入图片描述
在这里插入图片描述
版本实体类:

public class SoftwareVersion extends SuperEntity {private static final long serialVersionUID = 1L;@Schema(description = "版本名称")private String name;@Schema(description = "版本描述")private String description;@Schema(description = "板块数量")private Integer sectionNumber;@Schema(description = "栏目数量")private Integer columnNumber;@Schema(description = "是否授权全部工具:0-否;1-是")private Boolean toolAll;@Schema(description = "仅限以下工具,以逗号分割工具主键ID")private String toolOnly;@Schema(description = "是否授权全部布局:0-否;1-是")private Boolean layoutAll;@Schema(description = "授权布局【以JSON格式保存】")private String pageLayout;@Schema(description = "员工数量")private Integer employeesNumber;@Schema(description = "管理员数量")private Integer administratorNumber;
}

版本授权布局dto

@Data
@Schema(name = "VersionLayoutDto")
public class VersionLayoutDto {// 布局名称private String name;// 布局类型private Integer type;// 布局数量private Integer number;// 布局是否可用private Boolean able;}

代码实现:

public class Demo {public static void main(String[] args) {List<VersionLayoutDto> versionLayoutDtoList = new ArrayList<>(4);VersionLayoutDto versionLayoutDto1 = new VersionLayoutDto();versionLayoutDto1.setName("轮播");versionLayoutDto1.setType(1);versionLayoutDto1.setNumber(6);versionLayoutDto1.setAble(true);VersionLayoutDto versionLayoutDto2 = new VersionLayoutDto();versionLayoutDto2.setName("通栏");versionLayoutDto2.setType(2);versionLayoutDto2.setNumber(6);versionLayoutDto2.setAble(true);VersionLayoutDto versionLayoutDto3 = new VersionLayoutDto();versionLayoutDto3.setName("两栏");versionLayoutDto3.setType(3);versionLayoutDto3.setNumber(6);versionLayoutDto3.setAble(true);VersionLayoutDto versionLayoutDto4 = new VersionLayoutDto();versionLayoutDto4.setName("三栏");versionLayoutDto4.setType(4);versionLayoutDto4.setNumber(6);versionLayoutDto4.setAble(true);versionLayoutDtoList.add(versionLayoutDto1);versionLayoutDtoList.add(versionLayoutDto2);versionLayoutDtoList.add(versionLayoutDto3);versionLayoutDtoList.add(versionLayoutDto4);String jsonString = JSON.toJSONString(versionLayoutDtoList);System.out.println("List<VersionLayoutDto> versionLayoutDtoList: 转换成json字符串");System.out.println(jsonString);System.out.println();System.out.println("jsonString 转换成 List<VersionLayoutDto>");List<VersionLayoutDto> list = JSONArray.parseArray(jsonString, VersionLayoutDto.class);list.forEach(System.out::println);}
}

运行结果:

在这里插入图片描述

二、List<对象>转JSON字符串

String jsonString = JSON.toJSONString(versionLayoutDtoList);

三、JSON字符串转List<对象>

List<VersionLayoutDto> list = JSONArray.parseArray(jsonString, VersionLayoutDto.class);

总结

博客主要记录了List<对象>转JSON字符串以及JSON字符串转List<对象>,有啥错误或不足地方请指正,如果对你有所帮助,请一键三连。


http://www.ppmy.cn/embedded/100393.html

相关文章

美国RAKsmart大带宽服务器机房要求

RAKsmart是一家提供多种服务器托管和服务解决方案的公司&#xff0c;以其在美国的大带宽服务器闻名。这些服务器通常位于符合高标准的数据中心&#xff0c;如位于美国加州的机房&#xff0c;这些机房具有先进的基础设施和技术支持&#xff0c;以满足用户对于高性能、低延迟的需…

计算机毕业设计选题推荐-游戏比赛网上售票系统-Java/Python项目实战

✨作者主页&#xff1a;IT研究室✨ 个人简介&#xff1a;曾从事计算机专业培训教学&#xff0c;擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Python…

编程学习中的“知识宝库”打造秘籍

编程学习中的“知识宝库”打造秘籍 在编程学习的道路上&#xff0c;我们犹如航海家在知识的海洋中探索前行。而高效的笔记记录和整理方法&#xff0c;无疑就是那张珍贵的航海图&#xff0c;引领我们在浩瀚的编程知识海洋中找到方向。 一、为什么需要一个好的笔记系统 编程知…

Hive 记录

Hive从入门到精通&#xff0c;HQL硬核整理四万字&#xff0c;全面总结&#xff0c;附详细解析&#xff0c;赶紧收藏吧&#xff01;&#xff01;_hive的hql分析-CSDN博客 一&#xff0c;了解Hive 1&#xff0c;Hive的概念及架构 Hive是建立在Hadoop上的数据仓库基础架构。 提…

SpringBoot3

JDK 关注的新特性 搭建学习环境 有用的新特性 Java Record 看看 Record 怎么用 Instance Methods 静态方法 Static Method Record 的构造方法 Record 与 Lombok Record 实现接口 Local Record 嵌套 Record instanceof 判断 Record 类型 Switch 箭头表达式&#xff0c;新的 ca…

量子计算与未来的渗透技术(壹)

第一篇&#xff1a;量子计算对渗透测试的潜在影响 1. 量子计算概述 量子计算的基本原理&#xff1a;介绍量子比特&#xff08;qubits&#xff09;、叠加态、纠缠态等量子计算的核心概念。量子计算对传统计算的优势&#xff1a;解释量子计算在处理复杂计算任务上的潜在优势&am…

BUG——GT911上电后中断一直触发

版型&#xff1a;正点原子 I.MX6UL MINI板 屏幕&#xff1a;7寸 1024*600 ATK-MD0700R V1.4 我的建议是买7寸屏幕就不要Mini板&#xff0c;因为Mini板太小装不下7寸屏幕&#xff0c;你需要一个更大的板子 简介&#xff1a; 算是作为一个后来者对这一现象的补充。解决方案就…

Qt/C++控件实例 QWidget联合动画实现卷轴效果

显示特点 动态翻页效果&#xff1a;数字在更新时&#xff0c;会有一个从前一数字向下一数字过渡的翻页效果。这种过渡动画使得数字变化过程更加平滑和自然&#xff0c;避免了突然的跳变。 高对比度显示&#xff1a;每个数字的背景框颜色为红色&#xff0c;数字颜色为白色&…