我正在处理Windows表单,我想将目录文件夹和文件转换为Json字符串,如下所示,我正在使用牛顿json并有一个如下所示的json字符串,其中包含多个文件夹和文件,动态。
{
"folder": {
"name": "abc",
"folders": {
"folder": {
"name": "child abc",
"folders": {
"folder": {
"name": "child abcd"
}
},
"assets": {}
}
},
"assets": {
"asset": {
"folder_id": 14,
"uploaded_file": {
"content_type": "image/png",
"filename": "settings.png",
"file_data": "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAABsVJREFUeNrEV29"
}
}
}
}
}
///下面提到了我正在使用的类,如何获取具有多个文件夹和文件的 json 字符串。??帮帮我
public class Folders2
{
}
public class Assets
{
}
public class Folder2
{
public string name { get; set; }
public Folders2 folders { get; set; }
public Assets assets { get; set; }
}
public class Folders
{
public Folder2 folder { get; set; }
}
public class UploadedFile
{
public string content_type { get; set; }
public string filename { get; set; }
public string file_data { get; set; }
}
public class Asset
{
public int folder_id { get; set; }
public UploadedFile uploaded_file { get; set; }
}
public class Assets2
{
public Asset asset { get; set; }
}
public class Folder
{
public string name { get; set; }
public Folders folders { get; set; }
public Assets2 assets { get; set; }
}
public class RootObject
{
public Folder folder { get; set; }
}
///
我尝试在列表中获取目录和文件,谁能告诉我如何像上面的json字符串一样解析它
List<string> li = new List<string>();
li.Add(path);
foreach (string files in Directory.EnumerateFiles(path, "*.*", System.IO.SearchOption.TopDirectoryOnly))
{
bool exists2 = li.Exists(element => element.StartsWith(files));
if (exists2 == false)
{
li.Add(files);
}
}
foreach (string Folder in Directory.EnumerateDirectories(path, "*.*", System.IO.SearchOption.AllDirectories))
{
li.Add(Folder);
foreach (string file in Directory.EnumerateFiles(Folder, "*.*", System.IO.SearchOption.AllDirectories))
{
bool exists = li.Exists(element => element.StartsWith(file));
if (exists==false)
{
li.Add(file);
}
}
}
好吧,我可以看到两种从对象创建 JSON 字符串的方法。
-
) 手动创建 json 字符串,循环访问对象及其属性。
) 用于 IE。 JSON.NET (http://www.newtonsoft.com/json/help/html/Introduction.htm) 将对象序列化为 json。
我认为你也应该重新考虑类的设计,但那是另一回事。
在弗雷德里克· 的帮助和努力下,我完成了我想要的任务公共类文件夹根目录 1 {
public FolderSJ folder { get; set; }
}
public class FolderSJ
{
public FolderSJ()
{
assets =new List<AssetRoot> { };
folders = new List<FolderRoot1> { };
}
public string parent_id { get; set; }
public string name { get; set; }
public List<AssetRoot> assets { get; set; }
public List<FolderRoot1> folders { get; set; }
}
public class AssetSJ
{
public string filename { get; set; }
public string content_type { get; set; }
public string file_data { get; set; }
}
public class AssetRoot
{
public UploadedFileContent asset { get; set; }
}
public class UploadedFileContent
{
public AssetSJ uploaded_file { get; set; }
}