如何将目录文件夹和文件转换为json字符串

时间:2015-01-27 09:22:29

标签: winforms json.net

我正在使用Windows Form,我想将目录文件夹和文件转换为Json字符串,如下所示,我正在使用newton 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);
                }
            }

        }

2 个答案:

答案 0 :(得分:0)

我可以看到两种从对象创建JSON字符串的方法。

  1. )通过迭代对象及其属性手动创建json-string。

  2. )用于IE。 JSON.NET(http://www.newtonsoft.com/json/help/html/Introduction.htm)将您的对象序列化为json。

  3. 但我认为你也应该重新思考课程的设计,但这是另一回事。

答案 1 :(得分:0)

在Fredrik M的帮助和努力下,我完成了我想要的任务 公共类FolderRoot1     {

    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; }
}