Json.net自定义JsonConvert for Dictionary []

问题描述 投票:2回答:1

我已成功使用自定义JsonConverter来处理字典中可能是字符串或对象的项目:

[JsonProperty("result", ItemConverterType = typeof(TableFieldOrStringConverter), )]
public Dictionary<string, TableField> Records { get; set; }

但是现在我有一个属性是一系列字典。

public Dictionary<string, TableField>[] Records { get; set; }

如何设置自定义转换器以将其应用于字典的值?

这与将转换器应用于字典项的值的大多数问题不同,因为我试图将转换器应用于字典数组中的字典项的值,并且JSonPropery属性似乎不允许。

c# .net json.net json-deserialization
1个回答
2
投票

我想我通过将属性分解为另一个类来解决这个问题。

[JsonObject]
public class TableUpdateResponse : IAPIResponse
{
    [JsonProperty("result")]
    public TableRow[] Records { get; set; }
}

[JsonDictionary(ItemConverterType = typeof(TableFieldOrStringConverter))]
public class TableRow : Dictionary<string, TableField>
{

}

[JsonObject]
public class TableField
{
    [JsonProperty("display_value")]
    public string DisplayValue { get; set; }

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

    /// <summary>
    /// Applicable when the field references another record
    /// </summary>
    [JsonProperty("link")]
    public string Link { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.