如何构建像目录树结构这样的对象?

时间:2017-07-20 07:29:23

标签: c# asp.net-web-api data-structures

我正在html demo here中构建一个文件管理器,我在c#中将文件路由作为字符串数组,就像那个["file.exe","folder1/",folder1/file.xml","folder1/subfolder1/","folder1/subfolder1/file.docx"]一样。问题是...如何将此数组传递给c#中的结构以在ASP Web API中返回?结构必须像那样

 [
    {
        "text": "folder1", 
        "iconCls": "constant folder icon",
        "items": [
            {
                "text": "subfolder1",
                "iconCls": "constant folder icon",
                "folderData":[
                    {
                       "icon": "constant file icon", 
                       "text": "file.docx"
                    }
                ]
            }
        ]
        "folderData":[
             {
                "icon": "constant file icon", 
                "text": "file.xml"
            }
        ]
    }
]

2 个答案:

答案 0 :(得分:0)

这可能是您的任务的足够对象:

public class FolderData
{
    public string Text { get; set; }
    public Image IconCls { get; set; }
}

public class Folder
{
    public string Text { get; set; }
    public Image IconCls { get; set; }
    public List<Folder> Items { get; set; }

    public List<FolderData> FolderDatas { get; set; }
}

答案 1 :(得分:0)

这相当于C#类:

public class Directory
{
    public string text { get; set; }
    public string iconCls { get; set; }
    public List<FolderData> folderData { get; set; }
    public List<Directory> items { get; set; }

    public class FolderData
    {
        public string icon { get; set; }
        public string text { get; set; }
    }

    public class Item
    {
        public string text { get; set; }
        public string iconCls { get; set; }
        public List<FolderData> folderData { get; set; }
    }
}

我使用一些有用的在线工具来处理json:

Json viewer

C# class generator from json