如何将JSON转换为C#类?

时间:2011-01-17 23:11:08

标签: c# .net wcf web-services json

我有一个复杂的JSON对象,我希望将其表示为C#类。我在名为“Form”的父类上有一个良好的开端,但是我如何表示不同类型的集合(参见下面的“元素”对象)?

这是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"
            }
        }
    ]
]
}

我开始上课的时间如下:

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

    public string action
    {
        get;
        set;
    }

    public string method
    {
        get;
        set;
    }

    public ??? elements
    {
        get;
        set;
    }

    public Form()
    {

    }
}

如何处理“elements”属性以获得所需的JSON输出?

我在web.config中使用带有这些属性的WCF 4.0:automaticFormatSelectionEnabled =“false”,defaultOutgoingResponseFormat =“Json”。任何帮助或想法将不胜感激。

4 个答案:

答案 0 :(得分:2)

如果您不能自由使用.NET 4中的动态类型,或者想利用静态类型提供的好处,则codeplex上的JSON Class Generator项目将在给定json输入字符串的情况下生成c#类。 (无耻的插件)我也从这个项目和slapped a web UI on it中获取了代码。

答案 1 :(得分:1)

哇。引人入胜的问题。也许使用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

或者我认为匿名类型可以使用内置的.NET JSON序列化程序进行序列化。

答案 2 :(得分:0)

如果你只是想确保所有这些未知数据被反序列化并且可以在将来某个时候重新序列化,我建议使用IExtensibleDataObject。

以下是一些可以帮助您入门的示例。希望这可以帮助! (如果你已经知道这一点并且正在寻找不同的东西......请告诉我!)

Forward-Compatible Data Contracts

Data Contract Versioning

Useful clarifying thread on the topic at MSDN forums

答案 3 :(得分:0)

您无需尝试手动创建类结构。

有时它也很令人沮丧。 :)

有一个Visual Studio命令可以使用(我认为vs2015及更高版本):

  1. 在新的班级文件上,单击菜单=>编辑=>选择性粘贴
  2. 选择“将JSON粘贴为类”

现在特别是在JSON中出现错误,您丢失了第一个“元素”对象的右花括号。

以下是更正的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"
          }
        }
      ]
    }
  ]
}

以及相应的类:

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