weixin_40576400 2021-12-04 18:13 采纳率: 80.8%
浏览 85
已结题

C#如何实现复杂嵌套JSON数据的增删改

问题遇到的现象和发生背景

我在学习中需要根据某条件对嵌套JSON中的实体类进行增删改处理,例如下面的主程序中,如果某一层的OperCalculationRules.Length > 0,我就需要删除这一层的OperationBean,然后添加新的OperationBean来代替,并且新的OperationBean可能是一个OperationBean组(即不止一个OperationBean),其内部嵌套的长度不等。

我想请教的是这种情况下代码如何写?即需要在主程序中实现以下功能:
"//删除b1"
"//需要用obj1替换b1 ,其中obj1是类似于b1的OperationBean组"

问题相关代码,请勿粘贴截图
//主程序
 private void x(CalculateTransferBean obj, List<OperationBean> obj1)
        {
            #region  读取参数
            string _DrawingItemNumber = obj.DrawingItemNumber;
            string _TemplateNo = obj.TemplateNo;
            string _ConfigurationParameter = obj.ConfigurationParameter;
            string _RingTemplateNo = obj.RingTemplateNo;
            #endregion
            List<OperationBean> _OperationBeanList = obj.OperationBeanList;
            if (_OperationBeanList.Count > 0)
            {
                foreach (OperationBean b0 in _OperationBeanList)   
                {
                    List<OperationBean> _OperationBeanList0 = b0.OperBeanList;
                        if (_OperationBeanList0.Count > 0)
                        {
                            foreach (OperationBean b1 in _OperationBeanList0) 
                            {
                            if (b1.OperCalculationRules.Length > 0)   //假如存在计算规则
                            {
                                //删除b1
                                //需要用obj1替换b1 ,其中obj1是类似于b1的OperationBean组
                            }
                            else
                            {
                                List<OperationBean> _OperationBeanList1 = b1.OperBeanList;
                                if (_OperationBeanList1.Count > 0)
                                {
                                    foreach (OperationBean b2 in _OperationBeanList1) 
                                    {


                                    }
                                }
                            }
                            }
                        }
                }
            }
        }

//实体类
public class CalculateTransferBean
    {
        public String MPMProcessNumber { set; get; }
        public String TemplateNo { set; get; }
        public List<OperationBean> OperationBeanList { set; get; }// 工序集合
    }

//实体类
public class OperationBean
    {
        public String OperNumber { set; get; }// 操作编号
        public String OperName { set; get; }// 操作名称
        public String OperType { set; get; }// 操作类型
        public String OperSymble { set; get; }// 操作代号
        public String OperLabel { set; get; }// 操作标签编号
        public String OperCalculationRules { set; get; }// 计算规则
        public String OperModifyFlag { set; get; }// 是否修改
        public String OperDescription { set; get; }// 操作详细说明(返回信息)
        public String OperTexingFuhao { set; get; }// 操作特性符号(返回信息)
        public List<OperationBean> OperBeanList { set; get; }// 参数集合
    }
运行结果及报错内容
我的解答思路和尝试过的方法

b1.OperDescription = "XX";
b1.OperSymble = "AA";

我想要达到的结果
  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2021-12-04 20:45
    关注

    不能foreach,可以用for,调用List类的remove或者removeAt可以从队列中移除对象。
    直接替换的话也不能用foreach,也是for,然后直接更改对应下标的OperationBean 对象就行,逻辑大概如下

    
            private void x(CalculateTransferBean obj, List<OperationBean> obj1)
            {
                #region  读取参数
                string _DrawingItemNumber = obj.DrawingItemNumber;
                string _TemplateNo = obj.TemplateNo;
                string _ConfigurationParameter = obj.ConfigurationParameter;
                string _RingTemplateNo = obj.RingTemplateNo;
    
                #endregion
                List<OperationBean> _OperationBeanList = obj.OperationBeanList;// _OperationBeanList.Remove
                if (_OperationBeanList.Count > 0)
                {
                    foreach (OperationBean b0 in _OperationBeanList)
                    {
                        List<OperationBean> _OperationBeanList0 = b0.OperBeanList;
                        if (_OperationBeanList0.Count > 0)
                        {
                            //要删除或者更换列表对象不能用foreach,迭代变量不能直接替换,但是可以修改内部属性值
                            for (var i = 0; i < _OperationBeanList0.Count;i++)
                            {
                                var b1 = _OperationBeanList0[i];
                                if (b1.OperCalculationRules.Length > 0)
                                {
                                    b1 = new OperationBean();
                                    //对新的b1属性进行赋值或者计算
                                    _OperationBeanList0[i] = b1;///////////更新这个下标的对象为新的OperationBean
                                }
                                else
                                {
                                    List<OperationBean> _OperationBeanList1 = b1.OperBeanList;
                                    if (_OperationBeanList1.Count > 0)
                                    {
                                        foreach (OperationBean b2 in _OperationBeanList1)
                                        {
    
                                        }
                                    }
                                }
                            }
                            /*foreach (OperationBean b1 in _OperationBeanList0)
                            {
                                if (b1.OperCalculationRules.Length > 0)   //假如存在计算规则
                                {
                                    //删除b1
                                    //需要用obj1替换b1 ,其中obj1是类似于b1的OperationBean组
                                }
                                else
                                {
                                    List<OperationBean> _OperationBeanList1 = b1.OperBeanList;
                                    if (_OperationBeanList1.Count > 0)
                                    {
                                        foreach (OperationBean b2 in _OperationBeanList1)
                                        {
    
                                        }
                                    }
                                }
                            }*/
                        }
                    }
                }
            }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月13日
  • 已采纳回答 12月5日
  • 修改了问题 12月4日
  • 创建了问题 12月4日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格