0

我正在尝试从具有 9 行的数据表 (tblEmployees) 创建一个嵌套的 JSON 对象。我已经生成了一个对象,但它只包含最后一个数据行的重复。我错过了什么吗?下面显示了我使用的类和代码片段。

Structure structure = new Structure();
RootObject r = new RootObject();

foreach (DataRow row in tblEmployees.Rows)
            {
                r.id = Int32.Parse(row["id"].ToString());
                r.project = row["project"].ToString();
                r.priority = row["priority"].ToString();
                r.Child = new Child();
                r.Child.activity = row["activity"].ToString();
                r.Child.resource = row["resource"].ToString();
                r.Child.location = row["location"].ToString();
                r.Child.type = row["type"].ToString();
                r.Child.algorithm = row["algorithm"].ToString();
                // step1.Add(lvl1);
                r.Child.Child2 = new Child2();
                r.Child.Child2.cost = row["cost"].ToString();
                // step2.Add(lvl2);
                r.Child.Child2.Child3 = new Child3();
                r.Child.Child2.Child3.filename = row["filename"].ToString();
                r.Child.Child2.Child3.username = row["username"].ToString();
                // step3.Add(lvl3);
                structure.RootObject.Add(r);

            }

public class Child3
    {
        public string filename { get; set; }
        public string username { get; set; }
    }

    public class Child2
    {
        public string cost { get; set; }
        public Child3 Child3 { get; set; }        
    }

    public class Child
    {
        public string activity { get; set; }
        public string resource { get; set; }
        public string location { get; set; }
        public string type { get; set; }
        public string algorithm { get; set; }
        public string effort { get; set; }
        public Child2 Child2 { get; set; }
    }

    public class RootObject
    {
        public int id { get; set; }
        public string project { get; set; }
        public string priority { get; set; }
        public Child Child { get; set; }
    }

    public class Structure
    {
        public List<RootObject> RootObject = new List<RootObject>();
    }
4

0 回答 0