如何将JSON转换为C#类?

8

我有一个复杂的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"
            }
        }
    ]
]
}

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()
    {

    }
}

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

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


你看到这个了吗?http://json.codeplex.com/ 同时...看起来元素将成为一个自定义对象,需要进行反序列化。 - timothyclifford
谢谢。我正在研究库中的“CustomCreationConverter”,并考虑使用DataSets。然而,我的主要关注点是将值清晰地持久化到数据库中,并轻松管理/修改它们。 - TruMan1
@TruMan1,为什么你还没有关闭这个问题? 你看到我下面的答案了吗? 还有其他答案也可以解决你的问题。 - cnom
5个回答

3

如果您不能使用.NET 4中的动态类型或想利用静态类型提供的好处,那么codeplex上的JSON类生成器项目将根据json输入字符串生成c#类。(不要介意我自己推销) 我还从这个项目中借鉴了代码,并在网页上添加了一个UI界面


有没有可能添加一个复选框,使其变成UpperCamelCase?(无耻的请求) - Rippo

1

动态类型是个好主意。将其映射到数据库会很不结构化,我想动态类型会很慢(反射?)。也许 DataSet 和 EntityFramework 是一个不错的选择。与其映射类,我应该先考虑架构... 谢谢! - TruMan1
我为这个困境提出了完全相反的方法,开了一个线程。仍然不确定应该先映射到类还是数据库:http://stackoverflow.com/questions/4724465/how-to-map-json-to-sql-schema - TruMan1

1

在 Visual Studio 中,打开顶部菜单并将您的 Json 粘贴到此处 输入图像描述

点击“特殊粘贴”>“将 Json 粘贴为类”,您的 Json 将自动转换为对象或类。 输入图像描述


1

您不需要手动创建类结构。

有时这也是相当令人沮丧的。:)

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

  1. 在新的类文件上,单击菜单 => 编辑 => 粘贴特殊
  2. 选择“将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; }
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接