从文件夹结构构建 json,反之亦然
我正在尝试从文件夹结构生成 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以剩下要做的最后一件事是将
path
转换为 JSON。希望有帮助!
So the last thing left to do is convert
paths
to JSON.Hope it helps!