操作Json对象的C#方法

  • json对象长这样
    复制代码
    {
      "UniqueName": {
        "Required": "true",
        "MaxLength": 10,
        "MixLength": 2,
        "Regex": " "
      },
      "Email": {
        "Required": "true",
        "MaxLength": 10,
        "MixLength": 2,
        "Regex": "^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$"
      }
    }
    复制代码

     

  • C#类长这样
    复制代码
      public class ValidatorOptin
            {
                public string Name { get; set; }
                public string Required { get; set; }
                public int MaxLength { get; set; }
                public int MixLength { get; set; }
                public string Regex { get; set; }
                //public string RegexScript { get; set; }//JavaScript的正则
                //public string RegexCsharp { get; set; }//c#的正则
                public string Stauts { get; set; }//1合格2不合格
                public string RturnMsg { set; get; }
            }
    复制代码

     

  • 使用JObject进行遍历
    复制代码
       List<ValidatorOptin> validatorOptins = new List<ValidatorOptin>(); 
     var jsonpars = JObject.Parse(jsonstr);
                //将json构建成List<ValidatorOption>
                foreach (JToken child in jsonpars.Children())
                {
                    ValidatorOptin validatorOptin = new ValidatorOptin();
                    var property1 = child as JProperty;
                    validatorOptin.Name = property1.Name;
                    //Console.WriteLine(property1.Name + ":" + property1.Value);
                    foreach (JToken grandChild in child)
                    {
                        foreach (JToken grandGrandChild in grandChild)
                        {
                            var property = grandGrandChild as JProperty;
                            if (property != null)
                            {
                                switch (property.Name)
                                {
                                    case "Required":
                                        validatorOptin.Required = property.Value.ToString();
                                        break;
                                    case "MaxLength":
                                        validatorOptin.MaxLength = int.Parse(property.Value.ToString());
                                        break;
                                    case "MixLength":
                                        validatorOptin.MixLength = int.Parse(property.Value.ToString());
                                        break;
                                    case "Regex":
                                        validatorOptin.Regex = property.Value.ToString();
                                        break;
                                    default:
                                        break;
                                }
                                //Console.WriteLine(property.Name + ":" + property.Value);
                            }
                        }
                    }
                    validatorOptins.Add(validatorOptin);
                }
    复制代码

     

     

posted @   雨V幕  阅读(447)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示