How to convert JSON to C# classes?

后端 未结 4 1940

I have a complex JSON object that I want represent as C# class. I have a head start on the parent class called \"Form\", but how can I represent a collection for different t

4条回答
  •  天涯浪人
    2020-12-19 07:34

    You do not need to try and create the class structure manually.

    Sometimes it is rather frustrating too. :)

    There is a visual studio command you can use (I think vs2015 and later):

    1. On a new class file click Menu => Edit => Paste Special
    2. Select "Paste JSON as Classes"

    Now specifically in your JSON there is an error, you are missing the closing curly-brace of first "element" object.

    Below is the corrected JSON:

    {
      "action": "index.html",
      "method": "post",
      "elements": [
        {
          "type": "fieldset",
          "caption": "User information",
          "elements": [
            {
              "name": "email",
              "caption": "Email address",
              "type": "text",
              "placeholder": "E.g. user@example.com",
              "validate": {
                "email": true
              }
            },
            {
              "name": "password",
              "caption": "Password",
              "type": "password",
              "id": "registration-password",
              "validate": {
                "required": true,
                "minlength": 5,
                "messages": {
                  "required": "Please enter a password",
                  "minlength": "At least {0} characters long"
                }
              }
            },
            {
              "name": "password-repeat",
              "caption": "Repeat password",
              "type": "password",
              "validate": {
                "equalTo": "#registration-password",
                "messages": {
                  "equalTo": "Please repeat your password"
                }
              }
            },
            {
              "type": "radiobuttons",
              "caption": "Sex",
              "name": "sex",
              "class": "labellist",
              "options": {
                "f": "Female",
                "m": "Male"
              }
            }
          ]
        }
      ]
    }
    

    And the corresponding Classes:

    public class Rootobject
    {
        public string action { get; set; }
        public string method { get; set; }
        public Element[] elements { get; set; }
    }
    
    public class Element
    {
        public string type { get; set; }
        public string caption { get; set; }
        public Element1[] elements { get; set; }
    }
    
    public class Element1
    {
        public string name { get; set; }
        public string caption { get; set; }
        public string type { get; set; }
        public string placeholder { get; set; }
        public Validate validate { get; set; }
        public string id { get; set; }
        public string _class { get; set; }
        public Options options { get; set; }
    }
    
    public class Validate
    {
        public bool email { get; set; }
        public bool required { get; set; }
        public int minlength { get; set; }
        public Messages messages { get; set; }
        public string equalTo { get; set; }
    }
    
    public class Messages
    {
        public string required { get; set; }
        public string minlength { get; set; }
        public string equalTo { get; set; }
    }
    
    public class Options
    {
        public string f { get; set; }
        public string m { get; set; }
    }
    

提交回复
热议问题