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

时间:2014-09-24 06:13:26

标签: c# .net json json.net

我在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"
              }
            ]
          }
        ]
      }
    ]
  }
]

Json2CSharp输出:

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

2 个答案:

答案 0 :(得分:2)

只要结构完全相同,你就应该只需要:

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

依赖于大多数序列化程序如果是null将完全忽略该列表的事实。

例如,通过Jil

List<Node> nodes = Jil.JSON.Deserialize<List<Node>>(json);

并序列化:

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

答案 1 :(得分:0)

当你说C# object时,我想认为不涉及自定义类。 您可以使用dynamic

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的代码here

相关问题