当前位置:   article > 正文

解析大JSON串复杂数据结构并获取某一个节点数据_复杂的json字符串 获取其中某个字段的方法

复杂的json字符串 获取其中某个字段的方法

问题:

1.数据量大,JSON 串复杂

2.数据转换实体字段命名名不一致,但是数据类型一样。

解决方案:

  1. public static void main(String[] args) {
  2. String json = "[{\"name\":\"A\",\"children\":[{\"name\":\"B\",\"children\":[{\"name\":\"D\",\"children\":[{\"name\":\"G\"},{\"name\":\"H\"}]}]},{\"name\":\"C\",\"children\":[{\"name\":\"E\",\"children\":[{\"name\":\"I\"}]},{\"name\":\"F\"}]}]}]";
  3. List<TreeNode> treeNodes = JSON.parseArray(json, TreeNode.class);
  4. System.out.println(flatTree(treeNodes).stream().map(TreeNode2::getCode).collect(Collectors.toList()));
  5. System.out.println(flatTree(treeNodes));
  6. }
  7. //JSON 展平
  8. private static List<TreeNode2> flatTree(List<TreeNode> tree) {
  9. return Optional.ofNullable(tree).map(t -> t.stream()
  10. .flatMap(node ->
  11. {
  12. //类型转换
  13. List<TreeNode2> children = flatTree(node.getChildren());
  14. return Stream.concat(Stream.of(new TreeNode2(node.getName(),children)),children.stream());
  15. }
  16. )
  17. .collect(Collectors.toList())).orElse(Collections.emptyList());
  18. }
  19. @Data
  20. @AllArgsConstructor
  21. @NoArgsConstructor
  22. class TreeNode {
  23. private String name;
  24. private List<TreeNode> children;
  25. }
  26. @Data
  27. @AllArgsConstructor
  28. @NoArgsConstructor
  29. class TreeNode2 {
  30. private String code;
  31. private List<TreeNode2> children;
  32. }

前端代码演示 DEMO

  1. // A
  2. // / \
  3. // B C
  4. // / \ / \
  5. // D E F G
  6. // const tree1 = [
  7. // {
  8. // name: "A",
  9. // children: [
  10. // { name: "B", children: [{ name: "D" }, { name: "E" }] },
  11. // { name: "C", children: [{ name: "F" }, { name: "G" }] },
  12. // ],
  13. // },
  14. // ];
  15. // A
  16. // / \
  17. // B C
  18. // / / \
  19. // D E F
  20. // / \ \
  21. // G H I
  22. const tree = [
  23. {
  24. name: "A",
  25. children: [
  26. {
  27. name: "B",
  28. children: [{ name: "D", children: [{ name: "G" }, { name: "H" }] }],
  29. },
  30. {
  31. name: "C",
  32. children: [{ name: "E", children: [{ name: "I" }] }, { name: "F" }],
  33. },
  34. ],
  35. },
  36. ];
  37. // // 递归
  38. // function loopTree(tree) {
  39. // (tree || []).forEach((item) => {
  40. // // 前序遍历
  41. // // console.log(item.name);
  42. // loopTree(item.children);
  43. // // 后续遍历
  44. // console.log(item.name);
  45. // });
  46. // }
  47. // loopTree(tree);
  48. // console.log("------------------");
  49. // function sTree(tree) {
  50. // const arr = [...(tree || [])];
  51. // while (arr.length) {
  52. // // const item = arr.pop(); //前序遍历
  53. // const item = arr.shift(); //层级遍历
  54. // console.log(item.name);
  55. // arr.push(...(item.children || []));
  56. // // arr.push(...(item.children || []).reverse());
  57. // }
  58. // }
  59. // sTree(tree);
  60. // console.log("------------------");
  61. function flatTree(tree) {
  62. return (tree || []).flatMap((node) => [...flatTree(node.children), node]);
  63. }
  64. console.log(
  65. flatTree(tree)
  66. .map((v) => v.name)
  67. .join()
  68. );
  69. // 广度遍历 - 层级遍历
  70. // 深度遍历
  71. // 前序遍历(跟左右)
  72. // 中序遍历(左跟右) 自己了解
  73. // 后序遍历(左右跟)
  74. // len > 0 size > 0
  75. // 当 len = 1 size = 1
  76. // [0]
  77. // 当 len = 2 size = 1
  78. // [0, 0]
  79. // 当 len = 3 size = 1
  80. // [0, 0, 0]
  81. // 当 len = 2 size = 2
  82. // [0, 0]
  83. // [0, 1]
  84. // [1, 0]
  85. // [1, 1]
  86. // 当 len = 2 size = 3
  87. // [0, 0]
  88. // [0, 1]
  89. // [0, 2]
  90. // [1, 0]
  91. // [1, 1]
  92. // [1, 2]
  93. // [2, 0]
  94. // [2, 1]
  95. // [2, 2]
  96. // 当 len = 3 size = 2
  97. // [0, 0, 0]
  98. // [0, 0, 1]
  99. // [0, 1, 0]
  100. // [0, 1, 1]
  101. // [1, 0, 0]
  102. // [1, 0, 1]
  103. // [1, 1, 0]
  104. // [1, 1, 1]
  105. // 当 len = 3 size = 3
  106. // [0, 0, 0]
  107. // [0, 0, 1]
  108. // [0, 0, 2]
  109. // [0, 1, 0]
  110. // [0, 1, 1]
  111. // [0, 1, 2]
  112. // [0, 2, 0]
  113. // [0, 2, 1]
  114. // [0, 2, 2]
  115. // [1, 0, 0]
  116. // [1, 0, 1]
  117. // [1, 0, 2]
  118. // [1, 1, 0]
  119. // [1, 1, 1]
  120. // [1, 1, 2]
  121. // [1, 2, 0]
  122. // [1, 2, 1]
  123. // [1, 2, 2]
  124. // [2, 0, 0]
  125. // [2, 0, 1]
  126. // [2, 0, 2]
  127. // [2, 1, 0]
  128. // [2, 1, 1]
  129. // [2, 1, 2]
  130. // [2, 2, 0]
  131. // [2, 2, 1]
  132. // [2, 2, 2]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/977841
推荐阅读
相关标签
  

闽ICP备14008679号