Serialise List <dictionary <string,object>&gt; </dictionary <string,object>

时间:2015-01-12 08:29:52

标签: c# json serialization

我有一个字典列表,我想在c#中用J#序列化

 protected List<Dictionary<String, Object>> Details;

在另一个处理程序中,我想反序列化这个字典列表,一些提案?

1 个答案:

答案 0 :(得分:2)

您可以使用JSON.net轻松完成此操作。这是一个例子:

    List<Dictionary<String, Object>> Details = new List<Dictionary<string, object>>
    {
        new Dictionary<string,object>
        {
            {"abc" , "def"},
            {"123", 234}
        },
        new Dictionary<string,object>
        {
            {"abc1" , "def1"},
            {"1231", 2341}
        }
    };

        // serializing to: [{"abc":"def","123":234},{"abc1":"def1","1231":2341}]
        string json = JsonConvert.SerializeObject(Details);

        // de-serializing to a new List<Dictionary<String, Object>>.
        List<Dictionary<String, Object>> newDic = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(json);