操作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); }
stay hungry stay foolish!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!