Как преобразовать JSON в классы C #?

У меня есть сложный объект 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"
            }
        }
    ]
]
}

Класс, который у меня есть start выглядит так:

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 ". Мы будем благодарны за любую помощь или идеи.

6
задан TruMan1 17 January 2011 в 23:11
поделиться