简体   繁体   中英

C# Convert a Datatable into a JSON hierarchy

I have the following Datatable in C# that I would like to convert into a json string:

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

If the last 7 digits are zero, there is no parent but if the last 7 digits are not zero, the overlying element is the parent. The JSON output should look like:

[{
"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"
}]
}]

How can i get the desired result using C#? ? I know it is basic programming but I am having hard time with it.

UPDATE: I have done now the following

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

But the output look like:

[{
    "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"
    }]
}]

You need to populate a collection of parent first, and then fill the children :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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