C#应用Newtonsoft.Json操作json[2]-反序列化不定类型

在读json时,有时不知道对方的数据类型是什么样的,本文用Newtonsoft,把json反序列化为List<Dictionary<string, string>>,在某种情况下还是有用的。

private static List<Dictionary<string, string>> ConvertJson(string source)
{
    List<Dictionary<string, string>> ret = new List<Dictionary<string, string>>();
    var ret1 = JsonConvert.DeserializeObject(source);
    if (ret1 is JArray)
    {
        JArray array = ret1 as JArray;
        foreach (var inst in array)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            ret.Add(dic);
            if (inst.HasValues)
            {
                foreach (var child in inst.Children())
                {
                    if (child is JProperty)
                    {
                        JProperty jp = child as JProperty;
                        dic.Add(jp.Name, jp.Value.ToString());
                    }
                }

            }
        }
    }

    return ret;
}
posted @ 2016-03-28 16:00  wardensky  阅读(1260)  评论(0编辑  收藏  举报