[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 flashercs 于 2021-12-17 07:58 编辑

其实是个tree 的遍历;每组数据只读一次. 改了部分json 数据,包含2级以上目录. powershell脚本
  1. #Requires -Version 5
  2. $sJson = @'
  3. {
  4.   "data" : [
  5.      [ 0, 30, "安全防护", 1 ],
  6.      [ 30, 3030, "门禁", 2 ],
  7.      [ 30, 200327231, "楼宇自动化", 2 ],
  8.      [ 30, 200327211, "救灾用品", 2 ],
  9.      [ 30, 200001791, "急救箱", 2 ],
  10.      [ 30, 3009, "消防器材", 2 ],
  11.      [ 30, 200004310, "楼宇对讲", 2 ],
  12.      [ 30, 200330186, "物联传感设备", 2 ],
  13.      [ 30, 200327212, "公共应急广播系统", 2 ],
  14.      [ 30, 3015, "交通安全", 2 ],
  15.      [ 3015, 42578, "交通安全_1", 3 ],
  16.      [ 42578, 9527, "交通安全_1_1", 4 ],
  17.      [ 30, 200327196, "保险柜/保险箱", 2 ],
  18.      [ 30, 200004311, "防盗报警设备", 2 ],
  19.      [ 30, 200328267, "安检防爆检测设备", 2 ],
  20.      [ 30, 200328217, "自卫防身及安保用品", 2 ],
  21.      [ 30, 200004309, "智能一卡通系统", 2 ],
  22.      [ 30, 200004343, "传输设备及电缆", 2 ],
  23.      [ 30, 200332185, "安防无人机和机器人", 2 ],
  24.      [ 30, 3011, "视频监控", 2 ],
  25.      [ 30, 3007, "劳动保护用品", 2 ],
  26.      [ 3007, 0, "劳动保护用品_0", 3 ],
  27.      [ 3007, 1, "劳动保护用品_1", 3 ],
  28.      [ 0, 21, "办公、文化及教育用品", 1 ],
  29.      [ 21, 211111, "艺术用品", 2 ],
  30.      [ 21, 100003131, "美工工具", 2 ],
  31.      [ 21, 100003135, "教学设备及用品", 2 ],
  32.      [ 21, 2213, "期刊与杂志", 2 ],
  33.      [ 21, 2209, "地图和地图集", 2 ],
  34.      [ 21, 2112, "办公用纸及纸制品", 2 ],
  35.      [ 21, 212002, "展示告示用品", 2 ],
  36.      [ 21, 200001562, "印刷制品", 2 ],
  37.      [ 21, 100003125, "学生用品", 2 ],
  38.      [ 21, 100003134, "胶带、胶水、包装带等", 2 ],
  39.      [ 21, 211106, "桌上收纳用品", 2 ],
  40.      [ 21, 100003155, "笔记本、拍纸本等书写用品", 2 ],
  41.      [ 21, 100003129, "办公装订用品", 2 ],
  42.      [ 21, 200001743, "钢笔,铅笔及书写工具", 2 ],
  43.      [ 21, 200004276, "文具贴纸/儿童贴纸", 2 ],
  44.      [ 21, 2139, "绘图工具", 2 ],
  45.      [ 21, 200652001, "财会用品", 2 ],
  46.      [ 21, 201236701, "书籍", 2 ],
  47.      [ 21, 201330702, "邮寄和装运", 2 ],
  48.      [ 21, 201338004, "文件夹、文件袋等收纳用品", 2 ],
  49.      [ 0, 509, "电话和通讯", 1 ],
  50.      [ 509, 100001205, "手机配件", 2 ],
  51.      [ 509, 100001204, "通信设备", 2 ],
  52.      [ 509, 200380144, "对讲机配附件", 2 ],
  53.      [ 509, 50906, "对讲机", 2 ],
  54.      [ 509, 201084002, "手机部件", 2 ],
  55.      [ 1, 7, "电脑和办公", 1 ],
  56.      [ 7, 200001083, "笔记本电脑部件及配件", 2 ],
  57.      [ 7, 70806, "电脑连线及接插件", 2 ],
  58.      [ 7, 708022, "电脑清洁用品", 2 ],
  59.      [ 7, 200001081, "电脑外设", 2 ],
  60.      [ 7, 200185144, "开发板及配件", 2 ],
  61.      [ 7, 100005329, "切换器", 2 ],
  62.      [ 7, 200003782, "办公电子", 2 ],
  63.      [ 7, 200001085, "平板电脑配件", 2 ],
  64.      [ 0, 44, "消费电子", 1 ],
  65.      [ 44, 629, "零配件", 2 ],
  66.      [ 44, 100000305, "摄影摄像", 2 ],
  67.      [ 44, 100000310, "游戏及配附件", 2 ],
  68.      [ 44, 100000308, "家用音视频设备", 2 ],
  69.      [ 44, 100000306, "便携音视频设备", 2 ],
  70.      [ 44, 200003803, "智能电子", 2 ]
  71.   ]
  72. }
  73. '@
  74. $oJson = ConvertFrom-Json -InputObject $sJson
  75. # return type: Dictionary[string,object]
  76. function ParseTreeData {
  77.   param (
  78.     [object[]]$Data
  79.   )
  80.   $stack = New-Object System.Collections.Stack
  81.   $nodeRoot = New-Object 'System.Collections.Generic.SortedDictionary[string,object]'
  82.   $stack.Push($nodeRoot)
  83.   foreach ($arr in $Data) {
  84.     $parentId = $arr[0].ToString()
  85.     $currentId = $arr[1].ToString()
  86.     $currentLevel = $arr[3]
  87.     # tree hierarchy from deep level to shallow level,simulate recurse call back
  88.     while ($stack.Count - 1 -gt $currentLevel) {
  89.       $null = $stack.Pop()
  90.     }
  91.     # if recurse call back to level 1, then back to 0, because node in level 1 MAY not appear
  92.     if ($stack.Count - 1 -eq $currentLevel -and $currentLevel -eq 1) {
  93.       $null = $stack.Pop()
  94.     }
  95.     # tree hierarchy from shallow level to deep level
  96.     if ($stack.Count - 1 -lt $currentLevel ) {
  97.       $peek = $stack.Peek()
  98.       if (-not $peek.ContainsKey($parentId)) {
  99.         $peek.Add($parentId, (New-Object 'System.Collections.Generic.SortedDictionary[string,object]'))
  100.       }
  101.       $stack.Push($peek[$parentId])
  102.     }
  103.     # add current item to the tree
  104.     $peek = $stack.Peek()
  105.     $peek.Add($currentId, (New-Object 'System.Collections.Generic.SortedDictionary[string,object]'))
  106.   }
  107.   return $nodeRoot
  108. }
  109. $nodeRoot = ParseTreeData -Data $oJson.Data
  110. 'Enumerate data hierarchy:'
  111. $nodeRoot["0"]["30"]
  112. $nodeRoot["0"]["30"]["3015"]
  113. $nodeRoot["0"]["30"]["3015"]["42578"]
  114. 'ConvertTo-Json:'
  115. $nodeRoot | ConvertTo-Json -Depth 100
  116. # 包含中文名称
  117. "`r`n #################### json data with name:"
  118. class Node {
  119.   [string]$Name
  120.   [System.Collections.Generic.SortedDictionary[string, Node]]$Nodes = [System.Collections.Generic.SortedDictionary[string, Node]]::new()
  121.   Node() { }
  122.   Node([string]$name) {
  123.     $this.Name = $name
  124.   }
  125. }
  126. function ParseTreeData2 {
  127.   param (
  128.     [object[]]$Data
  129.   )
  130.   $stack = New-Object System.Collections.Stack
  131.   $nodeRoot = New-Object Node
  132.   $stack.Push($nodeRoot)
  133.   foreach ($arr in $Data) {
  134.     $parentId = $arr[0].ToString()
  135.     $currentId = $arr[1].ToString()
  136.     $currentName = $arr[2]
  137.     $currentLevel = $arr[3]
  138.     # tree hierarchy from deep level to shallow level,simulate recurse call back
  139.     while ($stack.Count - 1 -gt $currentLevel) {
  140.       $null = $stack.Pop()
  141.     }
  142.     # if recurse call back to level 1, then back to 0, because node in level 1 MAY not appear
  143.     if ($stack.Count - 1 -eq $currentLevel -and $currentLevel -eq 1) {
  144.       $null = $stack.Pop()
  145.     }
  146.     # tree hierarchy from shallow level to deep level
  147.     if ($stack.Count - 1 -lt $currentLevel ) {
  148.       $peek = $stack.Peek().Nodes
  149.       if (-not $peek.ContainsKey($parentId)) {
  150.         $peek.Add($parentId, (New-Object Node))
  151.       }
  152.       $stack.Push($peek[$parentId])
  153.     }
  154.     # add current item to the tree
  155.     $peek = $stack.Peek().Nodes
  156.     $peek.Add($currentId, (New-Object Node -ArgumentList @($currentName)))
  157.   }
  158.   return $nodeRoot
  159. }
  160. $nodeRoot2 = ParseTreeData2 -Data $oJson.Data
  161. $nodeRoot2.Nodes["0"].Nodes["30"] | Format-Table -AutoSize
  162. $nodeRoot2.Nodes["0"].Nodes["21"].Nodes["100003125"] | Format-Table -AutoSize
  163. $nodeRoot2 | ConvertTo-Json -Depth 100
复制代码
1

评分人数

微信:flashercs
QQ:49908356

TOP

回复 15# 523066680


    数据来源应该可以控制是树的遍历顺序是 pre-order 或post-order; 固定pre-order 就可以用此法.
微信:flashercs
QQ:49908356

TOP

返回列表