直接上代码:
class Program
{
class aaa
{
public int a;
public string b;
public override string ToString()
{
return "b:" + b + " a:" + a;
}
}
static void Main(string[] args)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<int, aaa> kkkk = new Dictionary<int, aaa>();
kkkk[0] = new aaa() { a=0,b="a"};
kkkk[1] = new aaa() { a = 1, b = "b" };
kkkk[2] = new aaa() { a = 2, b = "c" };
var v = kkkk.ToDictionary(de => serializer.ConvertToType<string>(de.Key), de => de.Value);
string Contentjson = serializer.Serialize(v);
Console.WriteLine(Contentjson);
Dictionary<string, aaa> tt = serializer.Deserialize<Dictionary<string, aaa>>(Contentjson);
var vv = tt.ToDictionary(de => serializer.ConvertToType<int>(de.Key), de => de.Value);
foreach (var j in vv)
{
Console.WriteLine("key:" + j.Key + " " + j.Value.ToString());
}
Console.Read();
}
}
网友评论