jessemillman
jessemillman

Reputation: 47

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

Upvotes: 3

Views: 1908

Answers (2)

Andrei Bucurei
Andrei Bucurei

Reputation: 2438

When you say C# object I like to think that no custom defined classes are involved. You can use dynamic:

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

dynamic obj = serializer.Deserialize(e.Parameters, typeof(object));

Then you can access properties like that:

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

The code for DynamicJsonConverter can be found here

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062650

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

Upvotes: 3

Related Questions