解析大JSON串复杂数据结构并获取某一个节点数据

解析大JSON串复杂数据结构并获取某一个节点数据,第1张

问题:

1.数据量大,JSON 串复杂

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

解决方案:

    public static void main(String[] args) {
        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\"}]}]}]";
        List treeNodes = JSON.parseArray(json, TreeNode.class);
        System.out.println(flatTree(treeNodes).stream().map(TreeNode2::getCode).collect(Collectors.toList()));
        System.out.println(flatTree(treeNodes));
    }
    
//JSON 展平
private static List flatTree(List tree) {
        return Optional.ofNullable(tree).map(t -> t.stream()
                .flatMap(node ->
                        {
//类型转换
                            List children = flatTree(node.getChildren());
                            return Stream.concat(Stream.of(new TreeNode2(node.getName(),children)),children.stream());
                        }
                        )
                .collect(Collectors.toList())).orElse(Collections.emptyList());
    }


@Data
@AllArgsConstructor
@NoArgsConstructor
class TreeNode {
    private String name;
    private List children;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class TreeNode2 {
    private String code;
    private List children;
}

前端代码演示 DEMO

//       A
//     /  \
//    B    C
//   / \  / \
//  D  E F   G
// const tree1 = [
//   {
//     name: "A",
//     children: [
//       { name: "B", children: [{ name: "D" }, { name: "E" }] },
//       { name: "C", children: [{ name: "F" }, { name: "G" }] },
//     ],
//   },
// ];

//         A
//       /  \
//      B    C
//     /    / \
//    D    E   F
//   / \    \
//  G   H    I
const tree = [
  {
    name: "A",
    children: [
      {
        name: "B",
        children: [{ name: "D", children: [{ name: "G" }, { name: "H" }] }],
      },
      {
        name: "C",
        children: [{ name: "E", children: [{ name: "I" }] }, { name: "F" }],
      },
    ],
  },
];

// // 递归
// function loopTree(tree) {
//   (tree || []).forEach((item) => {
//     // 前序遍历
//     // console.log(item.name);
//     loopTree(item.children);
//     // 后续遍历
//     console.log(item.name);
//   });
// }
// loopTree(tree);
// console.log("------------------");
// function sTree(tree) {
//   const arr = [...(tree || [])];
//   while (arr.length) {
//     // const item = arr.pop(); //前序遍历
//     const item = arr.shift(); //层级遍历
//     console.log(item.name);
//     arr.push(...(item.children || []));
//     // arr.push(...(item.children || []).reverse());
//   }
// }
// sTree(tree);
// console.log("------------------");
function flatTree(tree) {
  return (tree || []).flatMap((node) => [...flatTree(node.children), node]);
}
console.log(
  flatTree(tree)
    .map((v) => v.name)
    .join()
);

// 广度遍历 - 层级遍历
// 深度遍历
// 前序遍历(跟左右)
// 中序遍历(左跟右) 自己了解
// 后序遍历(左右跟)

// len > 0 size > 0
// 当 len = 1 size = 1
// [0]
// 当 len = 2 size = 1
// [0, 0]
// 当 len = 3 size = 1
// [0, 0, 0]
// 当 len = 2 size = 2
// [0, 0]
// [0, 1]
// [1, 0]
// [1, 1]
// 当 len = 2 size = 3
// [0, 0]
// [0, 1]
// [0, 2]
// [1, 0]
// [1, 1]
// [1, 2]
// [2, 0]
// [2, 1]
// [2, 2]
// 当 len = 3 size = 2
// [0, 0, 0]
// [0, 0, 1]
// [0, 1, 0]
// [0, 1, 1]
// [1, 0, 0]
// [1, 0, 1]
// [1, 1, 0]
// [1, 1, 1]
// 当 len = 3 size = 3
// [0, 0, 0]
// [0, 0, 1]
// [0, 0, 2]
// [0, 1, 0]
// [0, 1, 1]
// [0, 1, 2]
// [0, 2, 0]
// [0, 2, 1]
// [0, 2, 2]
// [1, 0, 0]
// [1, 0, 1]
// [1, 0, 2]
// [1, 1, 0]
// [1, 1, 1]
// [1, 1, 2]
// [1, 2, 0]
// [1, 2, 1]
// [1, 2, 2]
// [2, 0, 0]
// [2, 0, 1]
// [2, 0, 2]
// [2, 1, 0]
// [2, 1, 1]
// [2, 1, 2]
// [2, 2, 0]
// [2, 2, 1]
// [2, 2, 2]

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/942952.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存