Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize JSON folder structure into C# Object

i have a folder structure in JSON that i need to deserialize into a c# object. i wanted to know how i can do this without setting up multiple child class objects (as there could be a huge number of child folders). I was thinking maybe a child object that inherits the parent object, or having an object that contains itself might be the way to go but i'm stumped!

Cheers!

JSON Structure:

 [
  {
    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 output:

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; }
}
like image 349
jessemillman Avatar asked Feb 04 '26 07:02

jessemillman


1 Answers

As long as the structure is the same all the way down, you should just need something like:

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

relying on the fact that most serializers will ignore the list completely if it is null.

For example, via Jil:

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

and to serialize:

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);
like image 108
Marc Gravell Avatar answered Feb 07 '26 04:02

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!