Java对象数组与JSON相互转换

最近在做项目的时候,需要将 json对象数据存入到数据库中去…

实体类

/**
 * @ClassName: CaseModulars
 * @Author: LW
 * @Date: 2021-02-27 17:00
 **/
@Data
public class CaseModular implements Serializable {
          
   
    private static final long serialVersionUID = 812860357094906855L;
    //病例模块编号
    private  String caseModularId;

    //模块标题
    private  String title;

}
// 将存入数据库中的JSon对象解析为 Java中的 List<T> 对象数组
public List<CaseModular> getImagesExamineJsonList() {
          
   
    if (this.imagesExamineJson != null) {
          
   
    //将数据库中的json字符串转换为 List对象
        List<CaseModular> caseModularList = JSON.parseObject(this.imagesExamineJson, new TypeReference<List<CaseModular>>() {
          
   });
        this.imagesExamineJsonList = caseModularList;
    }
    return this.imagesExamineJsonList;
}
//list对象数组 转换为 json字符串
String imagesExamineJson = JSON.toJSONString(caseModularList);
Mysql数据库中存储为 varchar :

[{
          
   "caseModularId":"07e7db5910ef434f9f5697382f9f7d45080","title":"20210302"}]
经验分享 程序员 微信小程序 职场和发展