user3235118
user3235118

Reputation: 25

convert data into tree structure json in C#

I have a data table as given below Table structure of given data,where I have four column Id,Name,Salary, RefId.

Where as in Refid we store the Id of the parent object.

the modal class for the data table is given below.

public class SalaryDetail
{
    public int Id { get; set; }
    public string Name { get; set; }
    public System.Nullable<int> Salary { get; set; }
    public System.Nullable<int> Refid { get; set; }
}

I need this table data into tree structure json format as below.

    {
  "result": true,
  "education_details": [
    {
      "ID": 1,
      "name": "James",
      "salary": 0,
      "refereceId": 0,
      "childs": [
        {
          "ID": 2,
          "name": "David",
          "salary": 0,
          "refereceId": 1,
          "childs": [
            {
              "ID": 3,
              "name": "Richard",
              "salary": 0,
              "refereceId": 2,
              "childs": [
                {
                  "ID": 4,
                  "name": "John",
                  "salary": 1000,
                  "refereceId": 3,
                  "childs": []
                },
                {
                  "ID": 5,
                  "name": "Robert",
                  "salary": 4000,
                  "refereceId": 3,
                  "childs": []
                },
                {
                  "ID": 7,
                  "name": "Kevin",
                  "salary": 0,
                  "refereceId": 3,
                  "childs": [
                    {
                      "ID": 8,
                      "name": "Jason",
                      "salary": 5000,
                      "refereceId": 7,
                      "childs": []
                    },
                    {
                      "ID": 9,
                      "name": "Mark",
                      "salary": null,
                      "refereceId": 7,
                      "childs": [
                        {
                          "ID": 10,
                          "name": "Thomas",
                          "salary": 1000,
                          "refereceId": 9,
                          "childs": []
                        },
                        {
                          "ID": 11,
                          "name": "Donald",
                          "salary": 1000,
                          "refereceId": 9,
                          "childs": []
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "ID": 6,
              "name": "Paul",
              "salary": 6000,
              "refereceId": 2,
              "childs": []
            }
          ]
        }
      ]
    }
  ]
}

I have also made three classes to make tree structure as below

public class Child
{
    public int ID { get; set; }
    public string name { get; set; }
    public int salary { get; set; }
    public int refereceId { get; set; }
    public List<Child> childs { get; set; }
}
public class EducationDetail
{
    public int ID { get; set; }
    public string name { get; set; }
    public int salary { get; set; }
    public int refereceId { get; set; }
    public List<Child> childs { get; set; }
}
public class RootObject
{
    public bool result { get; set; }
    public List<EducationDetail> education_details { get; set; }
}

Upvotes: 0

Views: 2713

Answers (1)

BWA
BWA

Reputation: 5764

You need classes like:

public class Child
{
    public int ID { get; set; }
    public string name { get; set; }
    public int salary { get; set; }
    public int refereceId { get; set; }
    public List<Child> childs { get; set; }
}

public class EducationDetail
{
    public int ID { get; set; }
    public string name { get; set; }
    public int salary { get; set; }
    public int refereceId { get; set; }
    public List<Child> childs { get; set; }
}

public class RootObject
{
    public bool result { get; set; }
    public List<EducationDetail> education_details { get; set; }
}

Fill it and serialize by JSON.NET. You can generate c# classes from JSON on json2csharp.com.

EDIT

Very simple example:

static void Main()
{
    RootObject ro = new RootObject()
    {
        result = true,
        education_details = new List<EducationDetail>()
    };

    EducationDetail ed = new EducationDetail()
    {
        ID = 1,
        name = "1",
        refereceId = 2,
        salary = 3,
        childs = null
    };

    ro.education_details.Add(ed);

    ed = new EducationDetail()
    {
        ID = 2,
        name = "2",
        refereceId = 2,
        salary = 3,
        childs = new List<Child>()
    };

    ro.education_details.Add(ed);

    Child c = new Child()
    {
        ID = 3,
        name = "3",
        refereceId = 2,
        salary = 3,
        childs = null
    };

    ed.childs.Add(c);

    string json = JsonConvert.SerializeObject(ro);
}

And you get this JSON:

{
    "result": true,
    "education_details": [{
        "ID": 1,
        "name": "1",
        "salary": 3,
        "refereceId": 2,
        "childs": null
    },
    {
        "ID": 2,
        "name": "2",
        "salary": 3,
        "refereceId": 2,
        "childs": [{
            "ID": 3,
            "name": "3",
            "salary": 3,
            "refereceId": 2,
            "childs": null
        }]
    }]
}

Upvotes: 3

Related Questions