免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 12403 | 回复: 5

linux tree 转json [复制链接]

论坛徽章:
0
发表于 2018-12-05 13:29 |显示全部楼层
想实现linux目录结构转成规定的json格式:


[root@test test]# tree
.
├── 1
│   └── 11
│       ├── 1111.txt
│       ├── 111.txt
│       ├── 11.txt
│       └── test1
│           └── test1.txt
├── 2
│   └── 21
│       └── 21.txt
└── test.txt




转换成:
[
    {
        'text': '1',
        'children': [
            {
                'text': '11',
                'children': [
                    {
                        'text': '11.txt'
                    },
                    {
                        'text': '111.txt'
                    },
                    {
                        'text': '1111.txt'
                    },
                                        {
                                        'text': 'test1',
                                        'children': [                                                                                               
                                                {
                                                        'text': '11.txt'
                                                }
                                        ]
                                        }
                ],
            },
        ]
    },
    {
        'text': '2',
        'children': [
            {
                'text': '21',
                'children': [
                    {
                        'text': '21.txt'
                    }
                ],
            },
        ]
    },
        {
        'text': 'test.txt'
    },
]

论坛徽章:
0
发表于 2018-12-05 13:36 |显示全部楼层
                  

论坛徽章:
8
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015小元宵徽章
日期:2015-03-06 15:58:18每日论坛发贴之星
日期:2015-06-08 22:20:00每日论坛发贴之星
日期:2015-06-08 22:20:00操作系统版块每日发帖之星
日期:2015-06-14 22:20:00数据库技术版块每日发帖之星
日期:2015-11-09 06:20:00数据库技术版块每日发帖之星
日期:2016-02-22 06:20:0015-16赛季CBA联赛之上海
日期:2017-01-01 23:58:53
发表于 2019-07-14 12:16 |显示全部楼层
你这贴 都没人回复啊

论坛徽章:
8
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015小元宵徽章
日期:2015-03-06 15:58:18每日论坛发贴之星
日期:2015-06-08 22:20:00每日论坛发贴之星
日期:2015-06-08 22:20:00操作系统版块每日发帖之星
日期:2015-06-14 22:20:00数据库技术版块每日发帖之星
日期:2015-11-09 06:20:00数据库技术版块每日发帖之星
日期:2016-02-22 06:20:0015-16赛季CBA联赛之上海
日期:2017-01-01 23:58:53
发表于 2019-07-14 20:26 |显示全部楼层
  1. <?php

  2. function dir2json($dir)
  3. {
  4.     $a = [];
  5.     if($handler = opendir($dir))
  6.     {
  7.         while (($content = readdir($handler)) !== FALSE)
  8.         {
  9.             if ($content != "." && $content != ".." && $content != "Thumb.db")
  10.             {
  11.                 if(is_file($dir."/".$content)) $a[] = $content;
  12.                                 else if(is_dir($dir."/".$content)) $a[$content] = dir2json($dir."/".$content);
  13.             }
  14.         }   
  15.         closedir($handler);
  16.     }
  17.     return $a;   
  18. }
  19. $argv1 = $argv[1];

  20. $argv2 = $argv[2];
  21. $argv3 = $argv[3];
  22. if (empty($argv3)) $argv3 = 0;
  23. else $argv3 = constant($argv3);

  24. if (empty($argv2)) {
  25.     echo "invalid arguments";
  26.         exit;
  27. }

  28. $arr = dir2json($argv1);
  29. $json = json_encode($arr, $argv3);
  30. file_put_contents($argv2, $json);
  31. ?>
复制代码



看下上面的符合要求不:

论坛徽章:
8
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015小元宵徽章
日期:2015-03-06 15:58:18每日论坛发贴之星
日期:2015-06-08 22:20:00每日论坛发贴之星
日期:2015-06-08 22:20:00操作系统版块每日发帖之星
日期:2015-06-14 22:20:00数据库技术版块每日发帖之星
日期:2015-11-09 06:20:00数据库技术版块每日发帖之星
日期:2016-02-22 06:20:0015-16赛季CBA联赛之上海
日期:2017-01-01 23:58:53
发表于 2019-07-14 20:28 |显示全部楼层
  1. <?php

  2. function dir2json($dir)
  3. {
  4.     $a = [];
  5.     if($handler = opendir($dir))
  6.     {
  7.         while (($content = readdir($handler)) !== FALSE)
  8.         {
  9.             if ($content != "." && $content != ".." && $content != "Thumb.db")
  10.             {
  11.                 if(is_file($dir."/".$content)) $a[] = $content;
  12.                                 else if(is_dir($dir."/".$content)) $a[$content] = dir2json($dir."/".$content);
  13.             }
  14.         }   
  15.         closedir($handler);
  16.     }
  17.     return $a;   
  18. }
  19. $argv1 = $argv[1];

  20. $argv2 = $argv[2];
  21. $argv3 = $argv[3];
  22. if (empty($argv3)) $argv3 = 0;
  23. else $argv3 = constant($argv3);

  24. if (empty($argv2)) {
  25.     echo "invalid arguments";
  26.         exit;
  27. }

  28. $arr = dir2json($argv1);
  29. $json = json_encode($arr, $argv3);
  30. file_put_contents($argv2, $json);
  31. ?>
复制代码


aa.png

论坛徽章:
0
发表于 2019-07-16 20:41 |显示全部楼层
[ 本帖最后由 csccyab 于 2019-07-17 17:16 编辑 ]\n\n

樓上是 php 吧,現分享 python 版本

import os, json

def path_convert(path):
   d = {}
   if os.path.isdir(path):
      d['text'] = os.path.basename(path)
      d['children'] = [ path_convert(os.path.join(path,x)) for x in sorted(os.listdir(path)) ]
   else:
      d['text'] = os.path.basename(path)
   return d

print(json.dumps([ path_convert(y) for y in sorted(os.listdir('.'))]



$ python path_to_json.py
[
    {
        "text": "1",
        "children": [
            {
                "text": "11",
                "children": [
                    {
                        "text": "11.txt"
                    },
                    {
                        "text": "111.txt"
                    },
                    {
                        "text": "1111.txt"
                    },
                    {
                        "text": "test1",
                        "children": [
                            {
                                "text": "test1.txt",
                                "children": []
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "text": "2",
        "children": [
            {
                "text": "21",
                "children": [
                    {
                        "text": "21.txt"
                    }
                ]
            }
        ]
    },
    {
        "text": "test.txt"
    }
]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP