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

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日

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛