在读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;
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20958.html