如何从文件夹结构创建 JSON 对象
How can I create aJSON Object from Folder Structure
我的文件夹结构如下:
- directory1
- subdirectory1
- sub-subdirectory1
- sub-subdirectory2
- subdirectory2
- subdirectory3
- directory2
- subdirectory1
- sub-subdirectory1
- sub-subdirectory2
- sub-subdirectory3
- sub-subdirectory2
- subdirectory2
- subdirectory3
我想在 C# 中自动获取文件夹结构,以便填充级联下拉列表,然后使用 JavaScript 将结构解析到下拉列表 HTML。这是我要根据我的目录创建的对象:
{
"directory1": {
"subdirectory1": ["sub-subdirectory1", "sub-subdirectory2", "sub-subdirectory3"],
"subdirectory2": ["sub-subdirectory1", "sub-subdirectory2", "sub-subdirectory3"],
"subdirectory3": ["sub-subdirectory1", "sub-subdirectory2", "sub-subdirectory3"]
},
"directory2": {
"subdirectory1": ["sub-subdirectory1", "sub-subdirectory2", "sub-subdirectory3"] ,
"subdirectory2": ["sub-subdirectory1", "sub-subdirectory2", "sub-subdirectory3"]
}
没有硬编码,如何从 C# 中的目录结构创建这个嵌套的 json 对象
如果您只对目录感兴趣,我会像这样序列化一个模型:
public class MyDirModel
{
public string Name {get; set;}
public List<MyDirModel> SubDirectories {get; set;} = new List<MyDirModel>();
}
这不会产生完全符合您的 JSON,但是 - 我认为 - 更好,因为您不会被限制在 3 层深度。
更具体地说,这可能看起来像:
[
{
"name": "directory1",
"subdirectories": [
{ "name": "subdirectory1",
"subdirectories": [
{ "name": "sub-subdirectory1", "subdirectories": [] },
{ "name": "sub-subdirectory2", "subdirectories": [] },
{ "name": "sub-subdirectory3", "subdirectories": [] }
]},
{ "name": "subdirectory2",
"subdirectories": []},
{ "name": "subdirectory3",
"subdirectories": []}
]
},
{
name: "directory2",
subdirectories: [ /* shortened for brevity */ ]
}
]
我的文件夹结构如下:
- directory1
- subdirectory1
- sub-subdirectory1
- sub-subdirectory2
- subdirectory2
- subdirectory3
- directory2
- subdirectory1
- sub-subdirectory1
- sub-subdirectory2
- sub-subdirectory3
- sub-subdirectory2
- subdirectory2
- subdirectory3
我想在 C# 中自动获取文件夹结构,以便填充级联下拉列表,然后使用 JavaScript 将结构解析到下拉列表 HTML。这是我要根据我的目录创建的对象:
{
"directory1": {
"subdirectory1": ["sub-subdirectory1", "sub-subdirectory2", "sub-subdirectory3"],
"subdirectory2": ["sub-subdirectory1", "sub-subdirectory2", "sub-subdirectory3"],
"subdirectory3": ["sub-subdirectory1", "sub-subdirectory2", "sub-subdirectory3"]
},
"directory2": {
"subdirectory1": ["sub-subdirectory1", "sub-subdirectory2", "sub-subdirectory3"] ,
"subdirectory2": ["sub-subdirectory1", "sub-subdirectory2", "sub-subdirectory3"]
}
没有硬编码,如何从 C# 中的目录结构创建这个嵌套的 json 对象
如果您只对目录感兴趣,我会像这样序列化一个模型:
public class MyDirModel
{
public string Name {get; set;}
public List<MyDirModel> SubDirectories {get; set;} = new List<MyDirModel>();
}
这不会产生完全符合您的 JSON,但是 - 我认为 - 更好,因为您不会被限制在 3 层深度。
更具体地说,这可能看起来像:
[
{
"name": "directory1",
"subdirectories": [
{ "name": "subdirectory1",
"subdirectories": [
{ "name": "sub-subdirectory1", "subdirectories": [] },
{ "name": "sub-subdirectory2", "subdirectories": [] },
{ "name": "sub-subdirectory3", "subdirectories": [] }
]},
{ "name": "subdirectory2",
"subdirectories": []},
{ "name": "subdirectory3",
"subdirectories": []}
]
},
{
name: "directory2",
subdirectories: [ /* shortened for brevity */ ]
}
]