How to convert JSON to C# classes?

How to convert JSON to C# classes?

convert json to c# string online
json to c# list
json to c# class newtonsoft
json to c# class visual studio
json to class java
deserialize json to object c#
deserialize json c#
json to csharp converter

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 types (see the "elements" object below)?

Here is the JSON object:

{
    "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"
            }
        }
    ]
]
}

The class I have start looks like this:

public class Form
{
    public Guid id
    {
        get;
        set;
    }

    public string action
    {
        get;
        set;
    }

    public string method
    {
        get;
        set;
    }

    public ??? elements
    {
        get;
        set;
    }

    public Form()
    {

    }
}

How do I handle the "elements" property to get the desired JSON output?

I am using WCF 4.0 with these atributes in the web.config: automaticFormatSelectionEnabled="false", defaultOutgoingResponseFormat="Json". Any help or ideas would be greatly appreciated.


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.

JSON to C# • quicktype, Analyzing sample data cross. Generate C# classes with Json.NET attributes from JSON, JSON Schema, and GraphQL queries. Generate C# NowInstall Visual  Well, almostwhile Visual Studio does it's best to convert the JSON to a C# class, you may find that you need to slightly tweak the naming, but overall it converts really well. If you work with XML, the option to paste XML and convert to a C# class is also available.


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.

How to Convert JSON object to Custom C# object?, An example of how to use it: public class User { public User(string json) { JObject jObject = JObject.Parse(json); JToken jUser = jObject["user"]; name = (string)  If you're using Visual Studio 2015, it has a built in JSON-to-class feature. Just open a new empty class file and Edit > Paste Special > JSON as classes. Works with XML too. Not always perfect but pretty good. The only thing it missed in this case was the attribute decoration on the ID variables. – PMV Oct 2 '16 at 20:39


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

JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from , JSON Utils is a site for generating C#, VB.Net, Javascript, Java and PHP classes from JSON. It will also clean up your JSON and show a data viewer to assist  JSON To C Sharp Class Converter helps you to convert JSON to Csharp class online.


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

JSON to C# Converter: Site 24x7 Tools, JSON to C# Converter. Free tool to generate C# code from json object. The attributes in json object are converted into methods in C# class. Options to browse  JSON Utils is a site for generating C#, VB.Net, Javascript, Java and PHP classes from JSON. It will also clean up your JSON and show a data viewer to assist you while you are developing


C# to JSON Converter: Convert C# classes to JSON, CSharp to JSON Converter. Convert C# classes to JSON objects. Generally though, you can use the JavascriptSerializer class as below, or for more flexibility, use any of the various Json frameworks out there (Jayrock JSON is a good one) for easy JSON manipulation.


JSON to C# class conversion using Paste Special in Visual Studio, If you're regularly serializing or deserializing JSON data, you've most likely hand-​coded a class containing your JSON schema to make life a little  Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Jobs Programming and related technical career opportunities


How to serialize and deserialize JSON using C#, For production use, you would typically accept the default value of false for this setting. The code examples refer to the following class and variants of it: C# Copy​. Serialize properties of derived classes. Serialization of a polymorphic type hierarchy is not supported. For example, if a property is defined as an interface or an abstract class, only the properties defined on the interface or abstract class are serialized, even if the runtime type has additional properties.