- 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); }