using LitJson;
using System.Collections.Generic;
namespace Common
{
public class LitJsonHelper
{
///
/// 递归遍历 LitJson 的任意层级,取出所有指定字段名的值
///
public static List FindAllValuesByKey(JsonData root, string targetKey)
{
var result = new List();
// 如果是对象
if (root.IsObject)
{
// 遍历所有键
foreach (string key in root.Keys)
{
// 找到目标字段 → 加入结果
if (key == targetKey && root[key] != null)
{
result.Add(root[key].ToString());
}
// 递归遍历子节点
if (root[key] != null)
{
result.AddRange(FindAllValuesByKey(root[key], targetKey));
}
}
}
// 如果是数组
else if (root.IsArray)
{
foreach (JsonData item in root)
{
result.AddRange(FindAllValuesByKey(item, targetKey));
}
}
return result;
}
}
}