/// <summary>
/// 获取json数据
/// </summary>
/// <param name="json"></param>
/// <param name="jsonDic"></param>
private void GetJsonDic(JToken json, ref Dictionary<string, string> jsonDic)
{
foreach (JToken item in json.Children())
{
JProperty jProperty = item as JProperty;
if (jProperty == null)
{
GetJsonDic(item, ref jsonDic);
}
else
{
if (jProperty.Value.Children().Count() > 0)
{
GetJsonDic(jProperty.Value, ref jsonDic);
}
else if (jProperty.Value is JObject)
{
GetJsonDic(jProperty.Value, ref jsonDic);
}
else
{
var itemValue = jProperty.Value.ToString();
var itemPath = jProperty.Path.ToString();
jsonDic.Add(itemPath, itemValue);
}
}
}
}
调用方法:
JObject dataJson = JObject.Parse("json字符串");
Dictionary<string, string> jsonDic = new Dictionary<string, string>();
GetJsonDic(dataJson, ref jsonDic);