【问题标题】:JSON data is not bound with Dictionary property during deserializationJSON 数据在反序列化期间未与 Dictionary 属性绑定
【发布时间】:2015-12-22 05:31:20
【问题描述】:

我正在使用 Nest 查询与使用 Newtonsoft.Json 的相应属性绑定的数据。

以下是未填充 JSON 数据的类的属性。

[JsonExtensionData]
IDictionary<long, ICollection<Tuple<string, byte[ ]>>> ImageMap { get; set; }

但是,其他人是正确绑定的。 我正在使用 Newtonsoft.Json 7.0.1。

【问题讨论】:

  • 请编辑您的问题以包含您尝试反序列化的 JSON。

标签: c# json elasticsearch json.net nest


【解决方案1】:

问题是您不恰当地使用了[JsonExtensionData] 属性。 [JsonExtensionData] 旨在用于从 JSON 中捕获您没有在类中明确定义属性的额外数据。要正确使用它,您的类中的字典必须声明为Dictionary&lt;string, object&gt;Dictionary&lt;string, JToken&gt;。 (一个简单的例子见How to serialize a Dictionary as part of its parent object using Json.Net。)

但是,在您的情况下,您的类中有一个非常具体的 ImageMap 属性,旨在从 JSON 中相应的 imageMap 属性中捕获数据。这根本不适合扩展数据的用例。去掉[JsonExtensionData]属性,换成[JsonProperty("imageMap")];那么它应该正确反序列化。

[JsonProperty("imageMap")]
public IDictionary<long, ICollection<Tuple<string, byte[]>>> ImageMap { get; set; }

小提琴:https://dotnetfiddle.net/05J7Wo

【讨论】:

  • 感谢您解释正确使用 JsonExtentionData。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多