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

时间:2016-03-30 09:11:34

标签: c# json

我有一个数据表,如下所示 Table structure of given data,其中我有四列Id,Name,Salary,RefId。

在Refid中,我们存储父对象的Id。

数据表的模态类如下所示。

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

我需要将此表数据转换为树结构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": []
            }
          ]
        }
      ]
    }
  ]
}

我还制作了三个类来构建树结构,如下所示

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

1 个答案:

答案 0 :(得分:3)

您需要以下课程:

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

填写并按JSON.NET序列化。 您可以在json2csharp.com上从JSON生成c#类。

修改

很简单的例子:

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

你得到这个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
        }]
    }]
}