gpt4 book ai didi

C# 序列化字典 到 JSON

转载 作者:可可西里 更新时间:2023-11-01 08:56:57 33 4
gpt4 key购买 nike

我正在尝试将字典序列化为 JSON,并出现以下异常:

new JavaScriptSerializer().Serialize(mydict)`

Type 'System.Collections.Generic.Dictionary`2[[System.UInt64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable`1[[System.UInt64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' is not supported for serialization/deserialization of a dictionary, keys must be strings or objects.

有没有简单的方法来做到这一点?也许通过 LINQ 或其他相对简洁的方式将 ulong 转换为字符串?

最佳答案

var dict = mapping.ToDictionary(item => item.Key.ToString(), item => item.Value.ToString());`

这将转换任何 Dictionary<K,V>Dictionary<string,string>然后序列化工作。

关于C# 序列化字典 <ulong,ulong> 到 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3685732/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com