C#将数据表转换为JSON层次结构

时间:2016-12-21 10:35:54

标签: c# .net json

我在C#中有以下数据表,我想将其转换为json字符串:

Nr        |Name |Parent
1000000000|data |NULL
1100000000|data1|NULL
1110000000|data2|NULL
1110100000|data3|1110000000
1110200000|data4|1110000000
1120000000|data5|NULL
1120100000|data6|1120000000

如果最后7位数为零,则没有父项,但如果最后7位数不为零,则覆盖元素为父项。 JSON输出应如下所示:

[{
"nr": "1000000000",
"name": "data",
"child": [{
    "nr": null,
    "name": null
}]
}, {
"nr": "1100000000",
"name": "data1",
"child": [{
    "nr": null,
    "name": null
}]
}, {
"nr": "1110000000",
"name": "data2",
"child": [{
    "nr": "1110100000",
    "name": "data3"
}, {
    "nr": "1110200000",
    "name": "data4"
}]
}, {
"nr": "1120000000",
"name": "data5",
"child": [{
    "nr": "1120100000",
    "name": "data6"
}]
}]

如何使用C#获得所需的结果? ?我知道这是基本的编程,但我很难用它。

更新: 我现在做了以下

public class Child
{
    public string nr { get; set; }
    public string name { get; set; }
}
public class RootObject
{
    public string nr { get; set; }
    public string name { get; set; }
    public List<Child> child { get; set; }
}

List<Daten> parent = new List<Daten>();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            var innerRow = dt.Rows[i]["Nr"];
            var objParent = new Daten();
            bool alreadyExists = parent.Any(x => x.nr.Contains(innerRow.ToString()));

            if (alreadyExists)
                continue;

            DataRow[] foundRows = dt.Select("[Nr]='" + innerRow + "'");

            for (int k = 0; k < foundRows.Count(); k++)
            {
                var objChild = new Bezirke();
                objChild.nr = foundRows[k]["Parent"].ToString();
                objChild.name = foundRows[k]["Name"].ToString();
                objParent.bezirke.Add(objChild);
            }

            objParent.nr = innerRow.ToString();
            objParent.name = dt.Rows[i]["Name"].ToString();
            parent.Add(objParent);
        }

        string json = JsonConvert.SerializeObject(parent);
        Response.Write(json);

但输出看起来像:

[{
    "nr": "1000000000",
    "name": "data",
    "child": [{
        "nr": "",
        "name": "data"
    }]
}, {
    "nr": "1100000000",
    "name": "data1",
    "child": [{
        "nr": "",
        "name": "data1"
    }]
}, {
    "nr": "1110000000",
    "name": "data2",
    "child": [{
        "nr": "",
        "name": "data2"
    }]
}, {
    "nr": "1110100000",
    "name": "data3",
    "child": [{
        "nr": "1110100000",
        "name": "data3"
    }]
}, {
    "nr": "1110200000",
    "name": "data4",
    "child": [{
        "nr": "1110200000",
        "name": "data4"
    }]
}, {
    "nr": "1120000000",
    "name": "data5",
    "child": [{
        "nr": "1120000000",
        "name": "data5"
    }]
}, {
    "nr": "1120100000",
    "name": "data6",
    "child": [{
        "nr": "1120100000",
        "name": "data6"
    }]
}]

1 个答案:

答案 0 :(得分:2)

您需要首先填充父集合,然后填充子集:

List<RootObject> data = new List<RootObject>();

for (int i = 0; i < dt.Rows.Count; i++)
{
    if (dt.Rows[i]["Parent"] == null)
        data.Add(new RootObject
        {
           nr= dt.Rows[i]["Nr"],
           name = dt.Rows[i]["Name"],
           child = new List<Child>()
        });
}

for (int i = 0; i < dt.Rows.Count; i++)
{
    if (dt.Rows[i]["Parent"] != null)
    {   
       var parent = data.FirstOrDefault(d => d.nr == dt.Rows[i]["Parent"]);

       if(parent != null)
            parent.child.Add(new Child
            {
                nr = dt.Rows[i]["Nr"],
                name = dt.Rows[i]["Name"]
            });
   }
}