| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using LitJson;
- using System.Collections.Generic;
- namespace Common
- {
- public class LitJsonHelper
- {
- /// <summary>
- /// 递归遍历 LitJson 的任意层级,取出所有指定字段名的值
- /// </summary>
- public static List<string> FindAllValuesByKey(JsonData root, string targetKey)
- {
- var result = new List<string>();
- // 如果是对象
- 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;
- }
- }
- }
|