• Json递归删除和修改节点


    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class TaskNode
    5. {
    6. private string taskNodeName;
    7. private string taskParams;
    8. private List taskNodeList;
    9. public TaskNode(string taskNodeName, string taskParams, List taskNodeList=null)
    10. {
    11. this.taskNodeName = taskNodeName;
    12. this.taskParams = taskParams;
    13. this.taskNodeList = taskNodeList;
    14. }
    15. public string TaskNodeName { get => taskNodeName; set => taskNodeName = value; }
    16. public string TaskParams { get => taskParams; set => taskParams = value; }
    17. public List TaskNodeList { get => taskNodeList; set => taskNodeList = value; }
    18. }
    19. public class JsonTestHandle : MonoBehaviour
    20. {
    21. private TaskNode taskNode;
    22. // Start is called before the first frame update
    23. void Start()
    24. {
    25. //taskNode = new TaskNode("hexin", "0");
    26. //List taskNodes = new List();
    27. //TaskNode taskNode1 = new TaskNode("hexin1", "1", new List { new TaskNode("小明", "2"), new TaskNode("小兰", "3") });
    28. //TaskNode taskNode2 = new TaskNode("hexin2", "1", new List { new TaskNode("张三", "2") });
    29. //taskNodes.Add(taskNode1);
    30. //taskNodes.Add(taskNode2);
    31. //taskNode.TaskNodeList = taskNodes;
    32. //taskNode.TaskNodeList.Add(new TaskNode("李四", "0", new List { new TaskNode("小明", "2"), new TaskNode("小兰", "3") }));
    33. //taskNode.TaskNodeList.Add(
    34. // new TaskNode("王五", "0", new List {
    35. // new TaskNode("王五的孩子", "1", new List {
    36. // new TaskNode("王五的孙子", "2", new List {
    37. // new TaskNode("小明", "2"), new TaskNode("小兰", "3")
    38. // })
    39. // })
    40. // })
    41. // );
    42. //taskNode.TaskNodeList.Add(
    43. // new TaskNode("王五", "0", new List {
    44. // new TaskNode("小明", "1", new List {
    45. // new TaskNode("王五的孙子", "2", new List {
    46. // new TaskNode("小明", "2"), new TaskNode("小兰", "3")
    47. // })
    48. // })
    49. // })
    50. // ); taskNode.TaskNodeList.Add(
    51. // new TaskNode("小明", "0", new List {
    52. // new TaskNode("小明", "1", new List {
    53. // new TaskNode("王五的孙子", "2", new List {
    54. // new TaskNode("小明", "2"), new TaskNode("小兰", "3")
    55. // })
    56. // })
    57. // })
    58. // );
    59. //JsonTools.SaveJsonToLocal(taskNode, "localDatas");
    60. }
    61. private string tempNodeStr = "小明";
    62. // Update is called once per frame
    63. void Update()
    64. {
    65. if (Input.GetKeyDown(KeyCode.P))
    66. {
    67. TaskNode taskNode= JsonTools.FromFile(Application.streamingAssetsPath + "/Json/localDatas.json");
    68. DeleteJsonNode(taskNode.TaskNodeList, tempNodeStr);
    69. Debug.Log("删除之后——:" + JsonTools.ToJson(taskNode));
    70. //JsonTools.SaveJsonToLocal(taskNode, "localDatas");
    71. }
    72. if (Input.GetKeyDown(KeyCode.L))
    73. {
    74. TaskNode taskNode = JsonTools.FromFile(Application.streamingAssetsPath + "/Json/localDatas.json");
    75. TaskNode node = new TaskNode("小明1", "12345", new List { new TaskNode("何鑫", "1212") });
    76. ChangeJsonNode(taskNode.TaskNodeList, tempNodeStr,node);
    77. Debug.Log("更新之后——:" + JsonTools.ToJson(taskNode));
    78. }
    79. }
    80. ///
    81. /// 更新节点
    82. ///
    83. ///
    84. ///
    85. ///
    86. void ChangeJsonNode(List taskNodeList, string nodeStr,TaskNode newTaskNode)
    87. {
    88. if (taskNodeList != null)
    89. {
    90. List temp = taskNodeList;
    91. for (int i = 0; i < temp.Count; i++)
    92. {
    93. if (temp[i].TaskNodeName.Equals(nodeStr))
    94. {
    95. temp[i] = newTaskNode;
    96. }
    97. else
    98. {
    99. if (temp[i].TaskNodeList != null)
    100. {
    101. List temp1 = temp[i].TaskNodeList;
    102. for (int j = 0; j < temp1.Count; j++)
    103. {
    104. if (temp1[j].TaskNodeName.Equals(nodeStr))
    105. {
    106. temp1[j] = newTaskNode;
    107. }
    108. else
    109. {
    110. ChangeJsonNode(temp1[j].TaskNodeList, nodeStr, newTaskNode);
    111. }
    112. }
    113. }
    114. }
    115. }
    116. }
    117. }
    118. ///
    119. /// 删除节点
    120. ///
    121. ///
    122. ///
    123. void DeleteJsonNode(List taskNodeList, string nodeStr)
    124. {
    125. if (taskNodeList != null)
    126. {
    127. List temp = taskNodeList;
    128. for (int i = 0; i < temp.Count; i++)
    129. {
    130. if (temp[i].TaskNodeName.Equals(nodeStr))
    131. {
    132. taskNodeList.Remove(temp[i]);
    133. }
    134. }
    135. for (int i = 0; i < temp.Count; i++)
    136. {
    137. if (temp[i].TaskNodeList != null)
    138. {
    139. List temp1 = temp[i].TaskNodeList;
    140. for (int j = 0; j < temp1.Count; j++)
    141. {
    142. if (temp1[j].TaskNodeName.Equals(nodeStr))
    143. {
    144. temp1.Remove(temp1[j]);
    145. }
    146. }
    147. for (int j = 0; j < temp1.Count; j++)
    148. {
    149. DeleteJsonNode(temp1[j].TaskNodeList, nodeStr);
    150. }
    151. }
    152. }
    153. }
    154. }
    155. }

  • 相关阅读:
    网络基础2(1)
    Linux中Locate命令查找不全
    mmap底层驱动实现(remap_pfn_range函数)
    【Docker命令】日常使用的Docker命令
    【WP】猿人学12_入门级js
    WorkPlus | 好用、专业、安全的局域网即时通讯及协同办公平台
    前端面试基础面试题——9
    涛思 TDengine 表设计及SQL
    Go类型全解:常量与变量大全!
    【深度学习】详解 Node2Vec原理(含代码实现讲解) | NLP中训练词向量的基本原理和常见方法 | 跳字模型(Skip-gram)| MLP的核心机制
  • 原文地址:https://blog.csdn.net/qq_41579634/article/details/134016768