本帖最后由 flashercs 于 2021-12-17 07:58 编辑
其实是个tree 的遍历;每组数据只读一次. 改了部分json 数据,包含2级以上目录. powershell脚本- #Requires -Version 5
- $sJson = @'
- {
- "data" : [
- [ 0, 30, "安全防护", 1 ],
- [ 30, 3030, "门禁", 2 ],
- [ 30, 200327231, "楼宇自动化", 2 ],
- [ 30, 200327211, "救灾用品", 2 ],
- [ 30, 200001791, "急救箱", 2 ],
- [ 30, 3009, "消防器材", 2 ],
- [ 30, 200004310, "楼宇对讲", 2 ],
- [ 30, 200330186, "物联传感设备", 2 ],
- [ 30, 200327212, "公共应急广播系统", 2 ],
- [ 30, 3015, "交通安全", 2 ],
- [ 3015, 42578, "交通安全_1", 3 ],
- [ 42578, 9527, "交通安全_1_1", 4 ],
- [ 30, 200327196, "保险柜/保险箱", 2 ],
- [ 30, 200004311, "防盗报警设备", 2 ],
- [ 30, 200328267, "安检防爆检测设备", 2 ],
- [ 30, 200328217, "自卫防身及安保用品", 2 ],
- [ 30, 200004309, "智能一卡通系统", 2 ],
- [ 30, 200004343, "传输设备及电缆", 2 ],
- [ 30, 200332185, "安防无人机和机器人", 2 ],
- [ 30, 3011, "视频监控", 2 ],
- [ 30, 3007, "劳动保护用品", 2 ],
- [ 3007, 0, "劳动保护用品_0", 3 ],
- [ 3007, 1, "劳动保护用品_1", 3 ],
- [ 0, 21, "办公、文化及教育用品", 1 ],
- [ 21, 211111, "艺术用品", 2 ],
- [ 21, 100003131, "美工工具", 2 ],
- [ 21, 100003135, "教学设备及用品", 2 ],
- [ 21, 2213, "期刊与杂志", 2 ],
- [ 21, 2209, "地图和地图集", 2 ],
- [ 21, 2112, "办公用纸及纸制品", 2 ],
- [ 21, 212002, "展示告示用品", 2 ],
- [ 21, 200001562, "印刷制品", 2 ],
- [ 21, 100003125, "学生用品", 2 ],
- [ 21, 100003134, "胶带、胶水、包装带等", 2 ],
- [ 21, 211106, "桌上收纳用品", 2 ],
- [ 21, 100003155, "笔记本、拍纸本等书写用品", 2 ],
- [ 21, 100003129, "办公装订用品", 2 ],
- [ 21, 200001743, "钢笔,铅笔及书写工具", 2 ],
- [ 21, 200004276, "文具贴纸/儿童贴纸", 2 ],
- [ 21, 2139, "绘图工具", 2 ],
- [ 21, 200652001, "财会用品", 2 ],
- [ 21, 201236701, "书籍", 2 ],
- [ 21, 201330702, "邮寄和装运", 2 ],
- [ 21, 201338004, "文件夹、文件袋等收纳用品", 2 ],
- [ 0, 509, "电话和通讯", 1 ],
- [ 509, 100001205, "手机配件", 2 ],
- [ 509, 100001204, "通信设备", 2 ],
- [ 509, 200380144, "对讲机配附件", 2 ],
- [ 509, 50906, "对讲机", 2 ],
- [ 509, 201084002, "手机部件", 2 ],
- [ 1, 7, "电脑和办公", 1 ],
- [ 7, 200001083, "笔记本电脑部件及配件", 2 ],
- [ 7, 70806, "电脑连线及接插件", 2 ],
- [ 7, 708022, "电脑清洁用品", 2 ],
- [ 7, 200001081, "电脑外设", 2 ],
- [ 7, 200185144, "开发板及配件", 2 ],
- [ 7, 100005329, "切换器", 2 ],
- [ 7, 200003782, "办公电子", 2 ],
- [ 7, 200001085, "平板电脑配件", 2 ],
- [ 0, 44, "消费电子", 1 ],
- [ 44, 629, "零配件", 2 ],
- [ 44, 100000305, "摄影摄像", 2 ],
- [ 44, 100000310, "游戏及配附件", 2 ],
- [ 44, 100000308, "家用音视频设备", 2 ],
- [ 44, 100000306, "便携音视频设备", 2 ],
- [ 44, 200003803, "智能电子", 2 ]
- ]
- }
- '@
- $oJson = ConvertFrom-Json -InputObject $sJson
- # return type: Dictionary[string,object]
- function ParseTreeData {
- param (
- [object[]]$Data
- )
- $stack = New-Object System.Collections.Stack
- $nodeRoot = New-Object 'System.Collections.Generic.SortedDictionary[string,object]'
- $stack.Push($nodeRoot)
- foreach ($arr in $Data) {
- $parentId = $arr[0].ToString()
- $currentId = $arr[1].ToString()
- $currentLevel = $arr[3]
- # tree hierarchy from deep level to shallow level,simulate recurse call back
- while ($stack.Count - 1 -gt $currentLevel) {
- $null = $stack.Pop()
- }
- # if recurse call back to level 1, then back to 0, because node in level 1 MAY not appear
- if ($stack.Count - 1 -eq $currentLevel -and $currentLevel -eq 1) {
- $null = $stack.Pop()
- }
- # tree hierarchy from shallow level to deep level
- if ($stack.Count - 1 -lt $currentLevel ) {
- $peek = $stack.Peek()
- if (-not $peek.ContainsKey($parentId)) {
- $peek.Add($parentId, (New-Object 'System.Collections.Generic.SortedDictionary[string,object]'))
- }
- $stack.Push($peek[$parentId])
- }
- # add current item to the tree
- $peek = $stack.Peek()
- $peek.Add($currentId, (New-Object 'System.Collections.Generic.SortedDictionary[string,object]'))
- }
- return $nodeRoot
- }
-
- $nodeRoot = ParseTreeData -Data $oJson.Data
- 'Enumerate data hierarchy:'
- $nodeRoot["0"]["30"]
- $nodeRoot["0"]["30"]["3015"]
- $nodeRoot["0"]["30"]["3015"]["42578"]
- 'ConvertTo-Json:'
- $nodeRoot | ConvertTo-Json -Depth 100
-
- # 包含中文名称
- "`r`n #################### json data with name:"
- class Node {
- [string]$Name
- [System.Collections.Generic.SortedDictionary[string, Node]]$Nodes = [System.Collections.Generic.SortedDictionary[string, Node]]::new()
- Node() { }
- Node([string]$name) {
- $this.Name = $name
- }
- }
- function ParseTreeData2 {
- param (
- [object[]]$Data
- )
- $stack = New-Object System.Collections.Stack
- $nodeRoot = New-Object Node
- $stack.Push($nodeRoot)
- foreach ($arr in $Data) {
- $parentId = $arr[0].ToString()
- $currentId = $arr[1].ToString()
- $currentName = $arr[2]
- $currentLevel = $arr[3]
- # tree hierarchy from deep level to shallow level,simulate recurse call back
- while ($stack.Count - 1 -gt $currentLevel) {
- $null = $stack.Pop()
- }
- # if recurse call back to level 1, then back to 0, because node in level 1 MAY not appear
- if ($stack.Count - 1 -eq $currentLevel -and $currentLevel -eq 1) {
- $null = $stack.Pop()
- }
- # tree hierarchy from shallow level to deep level
- if ($stack.Count - 1 -lt $currentLevel ) {
- $peek = $stack.Peek().Nodes
- if (-not $peek.ContainsKey($parentId)) {
- $peek.Add($parentId, (New-Object Node))
- }
- $stack.Push($peek[$parentId])
- }
- # add current item to the tree
- $peek = $stack.Peek().Nodes
- $peek.Add($currentId, (New-Object Node -ArgumentList @($currentName)))
- }
- return $nodeRoot
- }
- $nodeRoot2 = ParseTreeData2 -Data $oJson.Data
-
- $nodeRoot2.Nodes["0"].Nodes["30"] | Format-Table -AutoSize
- $nodeRoot2.Nodes["0"].Nodes["21"].Nodes["100003125"] | Format-Table -AutoSize
-
- $nodeRoot2 | ConvertTo-Json -Depth 100
复制代码
|