C#序列化字典< ulong,ulong>到JSON [英] C# Serialize Dictionary<ulong,ulong> to JSON
问题描述
new JavaScriptSerializer()。Serialize() mydict)`
键入'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]]'不支持字典的序列化/反序列化,键必须是字符串或对象。
有没有办法做到这一点?可能通过LINQ或者相对简单的方法将ulongs转换成字符串?
var dict = mapping.ToDictionary(item => item.Key.ToString(),item => item.Value.ToString());`
将任何字典< K,V>
转换为字典< string,string>
然后序列化工作。
I am trying to serialize a Dictionary to JSON, and get the following exception:
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.
Is there an easy way to do this? Maybe converting the ulongs to strings via LINQ or something relatively terse?
var dict = mapping.ToDictionary(item => item.Key.ToString(), item => item.Value.ToString());`
that will convert any Dictionary<K,V>
to Dictionary<string,string>
and serialization then works.
这篇关于C#序列化字典< ulong,ulong>到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!