简体   繁体   English

在C#中将数据转换为树结构json

[英]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. 我有一个数据表,如下所示给定数据的表结构 ,其中我有四列Id,Name,Salary,RefId。

Where as in Refid we store the Id of the parent object. 在Refid中,我们存储父对象的ID。

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. 我需要将此表数据转换为树结构json格式,如下所示。

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

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 . 填充并通过JSON.NET进行序列化。 You can generate c# classes from JSON on json2csharp.com . 您可以在json2csharp.com上从JSON生成c#类。

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: 然后您得到以下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
        }]
    }]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM