JSON.NET:如何序列化嵌套集合

rjb*_*ock 5 c# json.net

这让我疯了......我正在使用Json.net将List序列化为JSON.我期待这个JSON:

{
    "fieldsets": [
        {
            "properties": [
                {
                    "alias": "date",
                    "value": "2014-02-12T00:00:00"
                },
                {
                    "alias": "time",
                    "value": null
                }
            ],
            "alias": "eventDates",
            "disabled": false
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

但相反,我得到了这个:

{
    "fieldsets": [
        {
            "properties": [
                {
                    "values": [
                        {
                            "alias": "date",
                            "value": "2014-07-13T00:00:00"
                        },
                        {
                            "alias": "time",
                            "value": "Registration begins at 8:00 AM; walk begins at 9:00 AM"
                        }
                    ]
                }
            ],
            "alias": "eventDates",
            "disabled": false
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想要的"值"集合只是一个JSON数组,但我不能为我的生活弄清楚如何让它做到这一点.我在我的"属性"对象上有一个名为"values"的属性,所以我理解它为什么这样做,但我只需要直接数组,而不是JSON对象.

Ayd*_*din 10

对于该响应,您需要此类结构

public class Property
{
    [JsonProperty("alias")]
    public string Alias { get; set; }

    [JsonProperty("value")]
    public string Value { get; set; }
}

public class Fieldset
{
    [JsonProperty("properties")]
    public Property[] Properties { get; set; }

    [JsonProperty("alias")]
    public string Alias { get; set; }

    [JsonProperty("disabled")]
    public bool Disabled { get; set; }
}

public class Response
{
    [JsonProperty("fieldsets")]
    public Fieldset[] Fieldsets { get; set; }
}
Run Code Online (Sandbox Code Playgroud)