且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中将文件列表转换为JSON数组

更新时间:2023-01-16 11:25:06

使用JSON.net

Use JSON.net

首先,创建一个像这样的类

First, create a class like this

public class FileInformation
{
    public string name{get;set;}
    [JsonProperty(PropertyName = "mime-type")]
    public string mimeType{get;set;}
    public int size {get;set;}
}

然后

var list = new List<FileInformation>();

foreach (string filename in fileNames)
{
  FileInfo fileInfo = new FileInfo(fileName);

  string actualFileName = fileInfo.Name;
  string mimeType = MimeMapping.GetMimeMapping(fileInfo.Name);
  long size = FileInfo.length;


  list.Add(new FileInformation(){//SetProperties});
}

var yourJSONString = JsonConvert.SerializeObject(list);

就这样

从网站下载JSON.net http://james.newtonking.com/json 或通过nuget

Download JSON.net from the website http://james.newtonking.com/json or via nuget