How to convert JSON to C # classes?

I have a complex JSON object that I want to represent as a C # class. I started a parent class called "Form", but how can I present 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": "Eg 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'm starting out is as follows:

 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 use WCF 4.0 with these attributes in the web.config file: automaticFormatSelectionEnabled = "false", defaultOutgoingResponseFormat = "Json". Any help or ideas would be very helpful.

+6
json c # web-services wcf
source share
4 answers

Wow. A 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, which I think can be serialized with the built-in .NET JSON serializer.

+1
source share

If you are unable to use dynamic types from .NET 4 or want to take advantage of the benefits of static typing, the JSON Class Generator on codeplex will generate the C # classes specified by the json input string. (shameless plugin) I also took the code from this project, and hit the web interface on it .

+3
source share

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

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

Portable compatible data

Data Version Versions

Useful Help Topic on MSDN Forums

0
source share

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

Sometimes this is also quite unpleasant. :)

There is a Visual Studio command that you can use (I think vs2015 and later):

  1. In the new class file, click Menu => Edit => Paste Special
  2. Select "Insert JSON as Classes"

In particular, there is an error in your JSON; you are missing the closing curly brace of the first "element" object.

The following is the corrected JSON:

 { "action": "index.html", "method": "post", "elements": [ { "type": "fieldset", "caption": "User information", "elements": [ { "name": "email", "caption": "Email address", "type": "text", "placeholder": "Eg 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
source share

All Articles