1

I have an API that I am calling that returns back a set of JSON. This one in particular is to return a set of Projects to me. These projects can have multiple sub-projects. The structure is the same for all of the sub-projects as it is the main projects. I can't seem to find anything that shows me how to parse the JSON so that it gives me some sort of usable class in .NET so I can show it on my web page.

Here is the structure (condensed because the original is HUGE and renamed projects and numbers to make it easier to read):

{
"data": [
    {
        "id": 70000,
        "name": "Project 70000",
        "children": [ 
            {
                "id": 71000,
                "name": "Project 71000",
                "children": [
                    {
                        "id": 71100,
                        "name": "Project 71100",
                        "children": [
                            {
                                "id": 71110,
                                "name": "Project 71110",
                                "children": [
                                    {
                                        "id": 71111,
                                        "name": "Project 71111"
                                    },
                                    {
                                        "id": 71112,
                                        "name": "Project 71112"
                                    }
                                 ]
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "id": 80000,
        "name": "Project 80000",
        "children": [
           {
               "id": 81000,
               "name": "Project 81000"
           }
        ]
    }
]
}

What I think I want my class to look like (I may be wrong here) is this:

public class data 
{
    public List<project> { get; set; }    
}
public class project 
{
    public int id { get; set; }
    public string name { get; set; }
    public List<project> children { get; set; }
}

I am using JSON.NET but anything I've tried winds up not doing anything or leaves me with errors in which I cannot build the project. I've used JSON.NET in the past when there was a defined structure, but this one is throwing me for a loop!

2 Answers 2

6

You're very close. The issue seems to be that there is no property name for the list in your data class. I would name that property data (to match the property in the JSON), then rename the class to something meaningful that makes sense for what this data represents. Here I'm using ProjectTree for an example.

public class ProjectTree
{
    public List<Project> data { get; set; }
}

public class Project
{
    public int id { get; set; }
    public string name { get; set; }
    public List<Project> children { get; set; }
}

Then, deserialize to your ProjectTree class like this, and it should work fine:

var root = JsonConvert.DeserializeObject<ProjectTree>(json);

Fiddle: https://dotnetfiddle.net/5gmosp

Sign up to request clarification or add additional context in comments.

Comments

1

You could create a self relationship for example:

public class Data
{
    public List<Project> data { get; set; }
}

public class Project
{
    public int id { get; set; }
    public string name { get; set; }
    public List<Project> children { get; set; }
}

var _data = JsonConvert.DeserializeObject<Data>(json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.