C#序列化字典< ulong,ulong>到JSON [英] C# Serialize Dictionary<ulong,ulong> to JSON

查看:279
本文介绍了C#序列化字典< ulong,ulong>到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#序列化字典&lt; ulong,ulong&gt;到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆