【发布时间】:2021-04-13 21:11:39
【问题描述】:
存在具有单个动态字段的具体模型类。该字段可能包含数百个 COMPLEX 数据结构,我们无法始终知道它们是什么。
public class Message {
public dynamic MessageData { get; set; }
}
我们可以从邮递员那里保存这个json:
{
"messageData": {
"sdfghdfghjdfghdfg": {
"sdfgsdfsdfghs": [
{
"dfgfghkfghjfg": "4567456734573",
"dfjghjjkdehn": "fgjfghdfgjfhnfgh"
}
],
"hfgjkfguksdfgnuy": {}
}
}
}
我们的 mongo find 方法显示了这个结果:
{
{ "_id" : ObjectId("607605a7d42ba9fa20579745"),
"MessageData" : { "_t" : "Newtonsoft.Json.Linq.JArray, Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed", "_v" : [ { "_t" : "JObject", "_v" : [ { "_t" : "JProperty", "_v" : [ { "_t" : "JObject", "_v" : [ { "_t" : "JProperty", "_v" : [ { "_t" : "JArray", "_v" : [ { "_t" : "JObject", "_v" : [ { "_t" : "JProperty", "_v" : [ { "_t" : "JValue", "_v" : [ ] } ] }, { "_t" : "JProperty", "_v" : [ { "_t" : "JValue", "_v" : [ ] } ] } ] } ] } ] }, { "_t" : "JProperty", "_v" : [ { "_t" : "JObject", "_v" : [ ] } ] } ] } ] } ] } ] }
}
在获取数据时我们得到这个错误:
System.FormatException: An error occurred while deserializing the MessageData property of class EMRPatientDB.Models.Message: Invalid element: '_t'.
问题似乎是 MongoDB C# 驱动程序没有正确序列化 JSON?
也许我们想编写一个自定义序列化程序?但我还没有找到一个很好的例子来说明如何为 JSON 对象完成此操作。
【问题讨论】:
标签: c# mongodb serialization dynamic