我有:
1)
Dictionary<MyClass, double>
2)elasticsearch 7
3)巢7
我想通过以下方式在Elasticsearch数据库中编写字典:
client.IndexDocument(myDictionary).
我知道,我可以使用
List<KeyValuePair<MyClass, double>>
但是在我的工作中,我需要使用Dictionary。
最佳答案
您需要实现一个类型转换器。将MyClassConverter
替换为MyClass
,然后将[TypeConverter(typeof(MyClassConverter))]
属性添加到MyClass类声明中。这意味着,它将使用您定义的类型转换来按需序列化,而不是使用默认的ToString()。
链接"How to: Implement a Type Converter"显示了如何创建类型转换器。
显示答案的SO链接为:
Not ableTo Serialize Dictionary with Complex key using Json.net
关于c# - 如何将Dictionary <object,object>序列化为json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249165/