zoukankan      html  css  js  c++  java
  • 操作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);
                  }
  • 相关阅读:
    debug和release转载
    坐标系与基本图元(8)
    坐标系与基本图元(7)
    坐标系与基本图元(5)
    坐标系与基本图元(6)
    坐标系与基本图元(4)
    坐标系与基本图元(3)
    坐标系与基本图元(2)
    BZOJ 1090
    Xor
  • 原文地址:https://www.cnblogs.com/chongyao/p/9377457.html
Copyright © 2011-2022 走看看