C#端点以JSON形式返回目录和文件的嵌套列表 [英] C# endpoint giving back a nested list of directories and files as JSON

查看:264
本文介绍了C#端点以JSON形式返回目录和文件的嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是实现一个端点,该端点从某个根目录(例如,根目录)开始返回所有文件和目录的嵌套列表. C:\Temp.我编写了以下代码:

My goal is to implement an endpoint which gives back a nested list of all files and directories starting from a certain root directory, e.g. C:\Temp. I have written the following code:

namespace API.Controllers
{
    public class UploadController : BaseController
    {
        [Route("api/Uploaded", Order = -1)]
        [ResponseType(typeof(IEnumerable<string[]>))] // <- this has to be adjusted, I guess.
        public IHttpActionResult AutoUpload()
        {
            string[] entries = Directory.GetFileSystemEntries("C:\Temp", "*", SearchOption.AllDirectories);
            // <-- Here should come some conversion to a nested JSON.
            return Ok(entries); 
        }
    }
}

当我查询端点时,例如使用curl -X POST --header 'Accept: application/json' 'http://localhost:63291/api/Uploaded'时,响应是某种情况

When I am querying the endpoint, e.g. with curl -X POST --header 'Accept: application/json' 'http://localhost:63291/api/Uploaded' the response is something

[
 "C:\\\\Temp\\file1",
 "C:\\\\Temp\\dir1\\file2",
 "C:\\\\Temp\\dir1\\file3"
]

我想拥有的东西类似于以下内容

What I would rather like to have is something like the following

[
    { "~":
        [ "file1" ]
    },
    {
        "~/dir1":
                [
                     "file2" ,
                     "file3" 
               ]
    }
]

我确信将列表转换为嵌套JSON不会那么复杂,但是我不知怎么做.我担心,我缺少适当的搜索词.请帮忙!

I am sure it cannot be so complicated to convert my list into a nested JSON, but I somehow do not manage to do it. I fear, I am missing the appropriate search terms. Please help!

推荐答案

首先属性 [ResponseType]不会影响您的响应,仅仅是元数据,例如大张旗鼓地用来记录您的端点.

First of all Attribute [ResponseType] does not affect your response, it's just metadata that is used by for example swagger to document your endpoints.

Directory.GetFileSystemEntries如Greg所说,返回表示路径的集合(数组)od字符串.您需要将结果解析为所需的格式,以便让我看到类似于File类和包含FileDirectory之类的东西,但更好的方法是使用诸如FileInfo的内建类型.

Directory.GetFileSystemEntries as Greg said returns collection (array) od strings representing paths. You need to parse your results to format you want i see that is something like File class and Directory containing File but better approach it would be to use build in types like FileInfo.

DirectoryInfo类提供了一些可以使用的有用方法:

DirectoryInfo class provides some useful methods that you could use:

获取目录中的所有文件:

Get all files in directory: https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo.getfiles?view=netframework-4.8

获取目录中的所有目录: https://docs. microsoft.com/en-us/dotnet/api/system.io.directoryinfo.getdirectories?view=netframework-4.8

Get all directories in directory: https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo.getdirectories?view=netframework-4.8

或者同时获得两者: https://docs. microsoft.com/en-us/dotnet/api/system.io.directoryinfo.getfilesysteminfos?view=netframework-4.8

Or also get both: https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo.getfilesysteminfos?view=netframework-4.8

了解该方法返回的数据类型并充分利用它;)

Read about data types returned by that methods and use the best of it ;)

我认为您需要进行一些递归才能遍历所有子目录.

I think you will need to do some recursion to go through all subdirectories.

这篇关于C#端点以JSON形式返回目录和文件的嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆