美文网首页
解析jason(2)-Dictionary

解析jason(2)-Dictionary

作者: 价值投机168 | 来源:发表于2019-12-25 17:37 被阅读0次

直接上代码:

  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();
    }
}

相关文章

网友评论

      本文标题:解析jason(2)-Dictionary

      本文链接:https://www.haomeiwen.com/subject/qvacoctx.html