• 将list中的对象转换为另一个类型的对象


    1、问题描述

    今天写代码发现别人现成的方法返回的是user对象,但是我需要是userVo对象,这就需要将list里面的对象转换,不做任何改变,对别人代码没有影响,有方法,特此记录方便使用。

    2、上代码

      //答复通知书
                if (StringUtils.isNotEmpty(a.getEvidenceId())) {
                    List<Evidence> evidenceList = evidenceMapper.selectByIdList(Arrays.asList(a.getEvidenceId().split(CommonConstants.SPLIT_COMMON)));
                    if (CollectionUtils.isNotEmpty(evidenceList)) {
                        List<EvidenceVo> evidenceVoList = JSONObject.parseArray(JSONObject.toJSONString(evidenceList), EvidenceVo.class);
                        evidenceVoList.forEach(evidenceVo -> evidenceVo.setEvidencePath(fdfsUtil.getDownLoadUrl() + evidenceVo.getEvidencePath()));
                        noticeReplyVo.setEvidenceList(evidenceVoList);
                    }
                }
    
                //其他附件材料
                if (StringUtils.isNotEmpty(a.getOtherEvidenceId())) {
                    List<Evidence> evidenceList = evidenceMapper.selectByIdList(Arrays.asList(a.getOtherEvidenceId().split(CommonConstants.SPLIT_COMMON)));
                    if (CollectionUtils.isNotEmpty(evidenceList)) {
                        List<EvidenceVo> evidenceVoList = JSONObject.parseArray(JSONObject.toJSONString(evidenceList), EvidenceVo.class);
                        evidenceVoList.forEach(evidenceVo -> evidenceVo.setEvidencePath(fdfsUtil.getDownLoadUrl() + evidenceVo.getEvidencePath()));
                        noticeReplyVo.setOtherEvidenceList(evidenceVoList);
                    }
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    把数据转为json格式就可以进行各种转换:

    1、对象转换

    JSONObject messageJo = messageList.getJSONObject(i);
    Message message = JSON.toJavaObject(messageJo, Message.class);
    
    
    • 1
    • 2
    • 3

    2、集合转换

     List<EvidenceVo> evidenceVoList = JSONObject.parseArray(JSONObject.toJSONString(evidenceList), EvidenceVo.class);
    
    • 1
  • 相关阅读:
    浏览器控制台报错Failed to load module script:解决方法
    使用 docker buildx 构建跨平台镜像
    BCC源码内容概览(2)
    k8s节点删除
    echarts 饼图 环形图 lable添加下划线
    传奇黑客斯诺登,现状如何了?
    编程随笔:控制复杂度是计算机编程的本质。
    混淆技术研究-OLLVM混淆-虚假控制流(BCF)
    我的第一个react.js 的router工程
    Java 动态代理原理图解 (附:2种实现方式详细对比)
  • 原文地址:https://blog.csdn.net/zouyang920/article/details/126407738