将JSON文件夹结构反序列化为C#对象

将JSON文件夹结构反序列化为C#对象,c#,.net,json,json.net,C#,.net,Json,Json.net,我有一个JSON中的文件夹结构,需要将其反序列化为c#对象。我想知道如何在不设置多个子类对象的情况下做到这一点(因为可能有大量的子文件夹)。我在想,也许一个继承父对象的子对象,或者拥有一个包含自身的对象可能是一种方法,但我被难住了 干杯 JSON结构: [ { type: "folder", name: "animals", path: "/animals", children: [ { type: "folder",

我有一个JSON中的文件夹结构,需要将其反序列化为c#对象。我想知道如何在不设置多个子类对象的情况下做到这一点(因为可能有大量的子文件夹)。我在想,也许一个继承父对象的子对象,或者拥有一个包含自身的对象可能是一种方法,但我被难住了

干杯

JSON结构:

 [
  {
    type: "folder",
    name: "animals",
    path: "/animals",
    children: [
      {
        type: "folder",
        name: "cat",
        path: "/animals/cat",
        children: [
          {
            type: "folder",
            name: "images",
            path: "/animals/cat/images",
            children: [
              {
                type: "file",
                name: "cat001.jpg",
                path: "/animals/cat/images/cat001.jpg"
              }, {
                type: "file",
                name: "cat001.jpg",
                path: "/animals/cat/images/cat002.jpg"
              }
            ]
          }
        ]
      }
    ]
  }
]
JSON2CHARP输出:

public class Child3
{
    public string type { get; set; }
    public string name { get; set; }
    public string path { get; set; }
}

public class Child2
{
    public string type { get; set; }
    public string name { get; set; }
    public string path { get; set; }
    public List<Child3> children { get; set; }
}

public class Child
{
    public string type { get; set; }
    public string name { get; set; }
    public string path { get; set; }
    public List<Child2> children { get; set; }
}

public class RootObject
{
    public string type { get; set; }
    public string name { get; set; }
    public string path { get; set; }
    public List<Child> children { get; set; }
}
公共类Child3
{
公共字符串类型{get;set;}
公共字符串名称{get;set;}
公共字符串路径{get;set;}
}
公营儿童2
{
公共字符串类型{get;set;}
公共字符串名称{get;set;}
公共字符串路径{get;set;}
公共列表子项{get;set;}
}
公营儿童
{
公共字符串类型{get;set;}
公共字符串名称{get;set;}
公共字符串路径{get;set;}
公共列表子项{get;set;}
}
公共类根对象
{
公共字符串类型{get;set;}
公共字符串名称{get;set;}
公共字符串路径{get;set;}
公共列表子项{get;set;}
}

只要结构始终相同,您就应该只需要以下内容:

public class Node {
    public string type {get;set;}
    public string name {get;set;}
    public string path {get;set;}
    public List<Node> children {get;set;}
}
并序列化:

var obj = new List<Node>
{
    new Node
    {
        type = "folder",
        name = "animals",
        path = "/animals",
        children = new List<Node>
        {
            new Node
            {
                type = "folder",
                name = "cat",
                path = "/animals/cat",
                children = new List<Node>
                {
                    new Node
                    {
                        type = "folder",
                        name = "images",
                        path = "/animals/cat/images",
                        children = new List<Node>
                        {
                            new Node
                            {
                                type = "file",
                                name = "cat001.jpg",
                                path = "/animals/cat/images/cat001.jpg"
                              },
                            new Node {
                                type = "file",
                                name = "cat001.jpg",
                                path = "/animals/cat/images/cat002.jpg"
                            }
                        }
                    }
                }
            }
        }
    }
};
string json = Jil.JSON.Serialize(obj, Jil.Options.PrettyPrint);
var obj=新列表
{
新节点
{
type=“文件夹”,
name=“动物”,
path=“/animals”,
儿童=新列表
{
新节点
{
type=“文件夹”,
name=“cat”,
path=“/anies/cat”,
儿童=新列表
{
新节点
{
type=“文件夹”,
name=“images”,
path=“/anies/cat/images”,
儿童=新列表
{
新节点
{
type=“file”,
name=“cat001.jpg”,
path=“/anives/cat/images/cat001.jpg”
},
新节点{
type=“file”,
name=“cat001.jpg”,
path=“/anies/cat/images/cat002.jpg”
}
}
}
}
}
}
}
};
字符串json=Jil.json.Serialize(obj,Jil.Options.PrettyPrint);
当你说
C#object
时,我喜欢认为不涉及自定义定义的类。 您可以使用
动态

var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });

dynamic obj = serializer.Deserialize(e.Parameters, typeof(object));
然后您可以访问如下属性:

string type = obj.type as string;
string name = obj.name as string;
...
可以找到
DynamicJsonConverter
的代码

string type = obj.type as string;
string name = obj.name as string;
...