如何将 JSON 转换为 C# 类?

问题描述 投票:0回答:5

我有一个复杂的 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. [email protected]",
            "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()
    {

    }
}

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

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

c# .net wcf web-services json
5个回答
3
投票

如果您没有使用 .NET 4 中的动态类型的自由,或者想利用静态类型提供的好处,codeplex 上的 JSON 类生成器 项目将在给定 json 输入字符串的情况下生成 c# 类。 (无耻的插件)我也从这个项目中获取了代码,并且在上面添加了一个 web UI.


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 序列化程序序列化的匿名类型。


1
投票

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

有时也很令人沮丧。 :)

有一个 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. [email protected]",
          "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; }
}

0
投票

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

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

向前兼容的数据合同

数据合同版本控制

MSDN 论坛上关于主题的有用的澄清线程


0
投票

转到顶部菜单上的 visual studio 并将您的 Json 粘贴到此处

点击Paste Special > Paste Json As Classes,你的json自动转换成对象或类。

© www.soinside.com 2019 - 2024. All rights reserved.