How to convert JSON to C# classes?

后端 未结 4 1941

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:19

    If you don't have the liberty of using dynamic types from .NET 4 or would like to leverage the benefits that static typing provide, the JSON Class Generator project on codeplex will generate c# classes given a json input string. (shameless plug) I've also taken code from this project and slapped a web UI on it.

    0 讨论(0)
  • 2020-12-19 07:23

    Wow. Fascinating question. Maybe use ExpandoObject / dynamic?

    http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx

    http://blogs.msdn.com/b/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx?PageIndex=4

    Or anonymous types I think are serializable with the built-in .NET JSON serializer.

    0 讨论(0)
  • 2020-12-19 07:28

    If you just want to make sure all this unknown data gets deserialized and can be reserialized at some point in the future, I suggest the usage of IExtensibleDataObject.

    Here are some samples to get you started. Hope this helps! (If you already knew this and were looking for something different...let me know!)

    Forward-Compatible Data Contracts

    Data Contract Versioning

    Useful clarifying thread on the topic at MSDN forums

    0 讨论(0)
  • 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; }
    }
    
    0 讨论(0)
提交回复
热议问题