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<对象>,有啥错误或不足地方请指正,如果对你有所帮助,请一键三连。

### 将列表(List)对象序列化为JSON字符串 在Java中,可以使用多种库将`List`对象序列化为JSON字符串。以下是几种常见的实现方式: #### 方法一:使用FastJSON FastJSON 是阿里巴巴开源的一个高性能 JSON 处理工具包。可以通过 `.toJSONString()` 方法轻松地将 Java 的 `List` 换为 JSON 字符串。 ```java import com.alibaba.fastjson.JSON; import java.util.Arrays; public class FastJSONExample { public static void main(String[] args) { String jsonString = JSON.toJSONString(Arrays.asList(1010, "哈哈哈", true, null)); System.out.println(jsonString); } } ``` 上述代码会输出如下结果[^1]: ```json [1010,"哈哈哈",true,null] ``` --- #### 方法二:使用Gson Google 提供的 Gson 库也可以用于将 Java 对象序列化为 JSON 字符串。通过 `toJson()` 方法即可完成此操作。 ```java import com.google.gson.Gson; import java.util.Arrays; public class GsonExample { public static void main(String[] args) { Gson gson = new Gson(); String jsonString = gson.toJson(Arrays.asList(1010, "哈哈哈", true, null)); System.out.println(jsonString); } } ``` 这段代码同样会生成相同的 JSON 输出[^2]: ```json [1010,"哈哈哈",true,null] ``` --- #### 方法三:手动构建JSON字符串 如果不想依赖第三方库,还可以手动拼接 JSON 字符串。不过这种方法容易出错,尤其是在处理复杂数据结构时。 ```java public class ManualJSONExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("["); sb.append("1010,"); sb.append("\"哈哈哈\","); sb.append("true,"); sb.append("null]"); String jsonString = sb.toString().replaceAll(",\\]", "\\]"); System.out.println(jsonString); } } ``` 该方法虽然可行,但在实际开发中不推荐使用,因为它缺乏灵活性和可维护性[^3]。 --- #### 方法四:其他语言中的实现(对比) 对于 Python 用户来说,可以直接利用内置的 `json` 模块进行序列化。然而需要注意的是,默认情况下某些自定义类可能无法被直接序列化,需要额外配置编码器[^5]。 ```python import json data_list = [1010, "哈哈哈", True, None] json_string = json.dumps(data_list) print(json_string) ``` 以上代码的结果与前面一致: ```json [1010,"哈哈哈",true,null] ``` 而在 .NET 平台下,则通常借助于 Newtonsoft.Json 或者 System.Text.Json 来完成类似的换工作[^4]。 --- ### 总结 无论采用哪种技术栈,都有相应的解决方案能够满足需求——即将一个普通的列表 (List) 换成标准格式化的 JSON 文本表示形式。具体选择取决于项目环境和个人偏好等因素影响下的权衡考量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值