c# - Json.net 自定义 JsonConvert for Dictionary<string,object>[]

标签 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 属性没有出现允许这样做。

最佳答案

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

[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; }
}

关于c# - Json.net 自定义 JsonConvert for Dictionary<string,object>[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52172037/

相关文章:

c# - 使用 ASP.NET MVC 作为 Web 服务

c# - Rsa加密解密中的错误长度错误

c# - 有时想要序列化 ​​Json 包括 [JsonIgnore]

c# - 解析的 JSON.NET 契约(Contract)不适用于内部对象

c# - 建议使用 c# 和 css 为用户提供选择以将页面字体大小更改为任何可能数字的方法

c# - ASP.NET MVC 模型绑定(bind)到列表中

c# - 删除 HtmlAgilityPack 中无用的 TextNode

.net - 是否有用于 .NET 的文本压缩库?

c# - 如果我担心内存使用,我应该避免使用 MVVM 吗?

c# - 使用 JSON.NET 反序列化一些 JSON