c# , 怎样将几个文件路径转换成文件夹的目录结构,然后生成json

时间:2022-08-06 12:27:47
现在问题是,之前做了文件夹上传,上传时用json保存了文件夹每个文件的相对路径。现在想通过每个文件的相对路径转换成文件夹的目录结构,传到前端用于jstree,展示文件夹的目录结构。挺急的,求个大佬帮忙!!!
下面是相对路径:

 string url1 = "/.DS_Store";
 string url2 = "/src/.DS_Store";
 string url3 = "/src/androidTest/java/thereisnospon/mcode/ApplicationTest.java";
 string url4 = "/src/main/.DS_Store"; 
 string url5 = "/src/main/AndroidManifest.xml";
 string url6 = "/src/main/java/thereisnospon/mcode/IndexActivity.java";
 string url7 = "/src/main/res/layout/activity_index.xml";

通过上面7个相对路径,生成文件夹的目录结构,然后转成json

3 个解决方案

#1


自顶!!!!

#2


你至少把你想转换的结果写一下吧

#3



[{
"name":"root",
"dirs":[{
"name":"src",
"dirs":[{
"name":"main",
"dirs":[{
"name":"java",
"dirs":[{
"name":"thereisnospon",
"dirs":[{
"name":"mcode",
"dirs":[],
"files":[{
"name":"IndexActivity.java"
}]
}],
"files":[]
},{
"name":"res",
"dirs":[{
"name":"layout",
"dirs":[],
"files":[{
"name":"activity_index.xml"
}]
}],
"files":[]
}],
"files":[]
}],
"files":[{
"name":".DS_Store"
},{
"name":"AndroidManifest.xml"
}]
},{
"name":"androidTest",
"dirs":[],
"files":[]
}],
"files":[{
"name":".DS_Store"
}]
}],
"files":[{
"name":".DS_Store"
}]
}]

构建成这样的json就可以了

#1


自顶!!!!

#2


你至少把你想转换的结果写一下吧

#3



[{
"name":"root",
"dirs":[{
"name":"src",
"dirs":[{
"name":"main",
"dirs":[{
"name":"java",
"dirs":[{
"name":"thereisnospon",
"dirs":[{
"name":"mcode",
"dirs":[],
"files":[{
"name":"IndexActivity.java"
}]
}],
"files":[]
},{
"name":"res",
"dirs":[{
"name":"layout",
"dirs":[],
"files":[{
"name":"activity_index.xml"
}]
}],
"files":[]
}],
"files":[]
}],
"files":[{
"name":".DS_Store"
},{
"name":"AndroidManifest.xml"
}]
},{
"name":"androidTest",
"dirs":[],
"files":[]
}],
"files":[{
"name":".DS_Store"
}]
}],
"files":[{
"name":".DS_Store"
}]
}]

构建成这样的json就可以了