c# - 如何将Dictionary <object,object>序列化为json

标签 c# json dictionary elasticsearch nest

我有:

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/

相关文章:

c# - 如何从 C# 调用 CPU 指令?

c# - MVVM 最佳实践 : communication between view models

c++ - 是否可以在不调用包含对象的析构函数的情况下清空 map 的内容?

python - 将python嵌套循环的结果存储到字典中

c# - 选择事务,只显示一行,但在数据库中有多行

javascript - 如何将 fixture/*.json 文件中的特定记录访问到 cypress 中的特定测试用例中?

java - 通过 id 从嵌套对象序列化

c# - 使用 json.net 支持 ISupportInitialize

python - 构建一个值为 defaultdict 的 defaultdict

c# - 为什么我的 log4net appender 没有缓冲?