Web Services 如何将JSON转换为C#类?

jdzmm42g  于 2023-04-21  发布在  C#
关注(0)|答案(5)|浏览(226)

我有一个复杂的JSON对象,我想表示为C#类。我在父类“Form”上有一个良好的开端,但我如何表示不同类型的集合(请参阅下面的“elements”对象)?
下面是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输出?
我正在使用WCF 4.0,在web.config中有以下属性:automaticFormatSelectionEnabled=“false”,defaultOutgoingResponseFormat=“Json”.任何帮助或想法将不胜感激.

3npbholx

3npbholx1#

如果你不能自由使用.NET 4中的动态类型,或者想利用静态类型提供的好处,codeplex上的JSON Class Generator项目将在给定json输入字符串的情况下生成c#类。

kmb7vmvb

kmb7vmvb2#

哇。有趣的问题。也许可以使用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序列化器序列化的匿名类型。

webghufk

webghufk3#

您不需要尝试手动创建类结构。
有时候也会很沮丧:)

有一个visual studio命令可以使用(我认为是vs2015和更高版本):

1.在一个新的类文件上单击Menu =〉Edit =〉Paste Special
1.选择“将JSON粘贴为类”
现在,特别是在JSON中,有一个错误,您缺少第一个“element”对象的结束花括号。
下面是正确的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; }
}
dphi5xsq

dphi5xsq4#

如果您只是想确保所有这些未知数据都被反序列化,并且可以在将来的某个时候被重新序列化,我建议使用IExtensibleDataObject。
这里有一些样本让你开始。希望这有帮助!(如果你已经知道这一点,并正在寻找不同的东西...让我知道!)
Forward-Compatible Data Contracts
Data Contract Versioning
Useful clarifying thread on the topic at MSDN forums

tp5buhyn

tp5buhyn5#

转到Visual Studio顶部菜单并将JSON粘贴到此处

点击选择性粘贴〉粘贴Json为类,你的json会自动转换为对象或类。

相关问题