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);
                  }
  • 相关阅读:
    在tmux中如何复制文本并粘贴到某处?
    linux下的用户密码文件/etc/shadow
    linux下openssl命令解析
    第 27 章 CSS 传统布局[下]
    第 27 章 CSS 传统布局[上]
    第 26 章 CSS3 动画效果
    第 25 章 CSS3 过渡效果
    第 24 章 CSS3 变形效果[下]
    第 23 章 CSS3 边框图片效果
    第 22 章 CSS3 渐变效果
  • 原文地址:https://www.cnblogs.com/chongyao/p/9377457.html
Copyright © 2011-2022 走看看