Json.net为Dictionary <string,object> []自定义JsonConvert

时间:2018-09-04 18:22:28

标签: c# .net json.net json-deserialization

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

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

但是现在我有一个由字典组成的属性。

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

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

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

1 个答案:

答案 0 :(得分: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; }
}
相关问题