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

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

文章目录


前言

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


一、工作需求

产品需求中有一个需求要求页面布局以JSON保存

版本实体类:

java 复制代码
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

java 复制代码
@Data
@Schema(name = "VersionLayoutDto")
public class VersionLayoutDto {

    // 布局名称
    private String name;

    // 布局类型
    private Integer type;

    // 布局数量
    private Integer number;

    // 布局是否可用
    private Boolean able;

}

代码实现:

java 复制代码
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字符串

java 复制代码
String jsonString = JSON.toJSONString(versionLayoutDtoList);

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

java 复制代码
List<VersionLayoutDto> list = JSONArray.parseArray(jsonString, VersionLayoutDto.class);

总结

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

相关推荐
和光同尘@28 分钟前
74. 搜索二维矩阵(LeetCode 热题 100)
数据结构·c++·线性代数·算法·leetcode·职场和发展·矩阵
zl_dfq1 小时前
数据结构之【顺序表简介】
数据结构·顺序表
code bean1 小时前
【C# 数据结构】队列 FIFO
开发语言·数据结构·c#
梅茜Mercy1 小时前
数据结构:栈和队列详解(下)
数据结构
Trouvaille ~2 小时前
【C++篇】树影摇曳,旋转无声:探寻AVL树的平衡之道
数据结构·c++·算法·蓝桥杯·计算机科学·平衡二叉树·avl
夏末秋也凉3 小时前
力扣-回溯-491 非递减子序列
数据结构·算法·leetcode
老菜鸡mou4 小时前
[OD E 100] 生成哈夫曼树
数据结构·c++
和光同尘@5 小时前
56. 合并区间 (LeetCode 热题 100)
c语言·开发语言·数据结构·c++·算法·leetcode·职场和发展
CS创新实验室5 小时前
计算机考研之数据结构:大 O 记号
数据结构·考研
wen__xvn7 小时前
每日一题洛谷P1914 小书童——凯撒密码c++
数据结构·c++·算法