从文件夹结构构建 json,反之亦然

发布于 2024-09-03 09:28:49 字数 1320 浏览 3 评论 0原文

我正在尝试从文件夹结构生成 json 表示形式。

文件夹/文件结构示例:

folder
|_ meta.xml
|_ subdir1
   |_ textfile1.txt
|_ subdir2
   |_ textfile2.txt 

手动生成此结构的 json 表示形式:

def builder = new net.sf.json.groovy.JsonGroovyBuilder()
def json = builder.dir {
    file(name: "meta.xml")
    folder(name: "subdir1") {
        file(name: "textfile.txt")
    }
    folder(name: "subdir2") {
        file(name: "textfile3.txt")
    }
}

generates:

{
   "dir":{
      "file":{
         "name":"meta.xml"
      },
      "folder":[
         {
            "name":"subdir1"
         },
         {
            "file":[
               {
                  "name":"textfile.txt"
               }
            ]
         },
         [
            {
               "name":"subdir2"
            },
            {
               "file":[
                  {
                     "name":"textfile3.txt"
                  }
               ]
            }
         ]
      ]
   }
}

Groovy 遍历文件夹结构的方式:

new File("./dir" ).eachFileRecurse{file ->
    println file
}

generates:

.\dir\meta.xml
.\dir\subdir1
.\dir\subdir1\textfile1.txt
.\dir\subdir2
.\dir\subdir2\textfile2.txt

但是如何将它们放在一起以自动生成呢?

I'm trying to generate a json representation from a folder structure.

Examplee folder/file structure:

folder
|_ meta.xml
|_ subdir1
   |_ textfile1.txt
|_ subdir2
   |_ textfile2.txt 

Manually generate the json representation of this structure:

def builder = new net.sf.json.groovy.JsonGroovyBuilder()
def json = builder.dir {
    file(name: "meta.xml")
    folder(name: "subdir1") {
        file(name: "textfile.txt")
    }
    folder(name: "subdir2") {
        file(name: "textfile3.txt")
    }
}

generates:

{
   "dir":{
      "file":{
         "name":"meta.xml"
      },
      "folder":[
         {
            "name":"subdir1"
         },
         {
            "file":[
               {
                  "name":"textfile.txt"
               }
            ]
         },
         [
            {
               "name":"subdir2"
            },
            {
               "file":[
                  {
                     "name":"textfile3.txt"
                  }
               ]
            }
         ]
      ]
   }
}

Groovy way of walking the folder structure:

new File("./dir" ).eachFileRecurse{file ->
    println file
}

generates:

.\dir\meta.xml
.\dir\subdir1
.\dir\subdir1\textfile1.txt
.\dir\subdir2
.\dir\subdir2\textfile2.txt

But how to put these together to generate this automatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-09-10 09:28:49
def paths = [:]
def listOfPaths = []

def access = {d, path ->
    if (d[path] == null) {
        d[path] = [:]
    } 
    return d[path]        
}

new File('./dir').eachFileRecurse{ file ->
    listOfPaths << file.toString().tokenize('/')
}

listOfPaths.each{ path ->
    def currentPath = paths
    path.each { step ->
        currentPath = access(currentPath, step)
    }
}

所以剩下要做的最后一件事是将 path 转换为 JSON。

希望有帮助!

def paths = [:]
def listOfPaths = []

def access = {d, path ->
    if (d[path] == null) {
        d[path] = [:]
    } 
    return d[path]        
}

new File('./dir').eachFileRecurse{ file ->
    listOfPaths << file.toString().tokenize('/')
}

listOfPaths.each{ path ->
    def currentPath = paths
    path.each { step ->
        currentPath = access(currentPath, step)
    }
}

So the last thing left to do is convert paths to JSON.

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文