Сериализация вложенного JSON
Я пытаюсь создать вложенный объект JSON из таблицы данных (tblEmployees) с 9 строками. Я создал объект, но он содержит только повторы последней строки данных. Я что-то пропустил? Классы, которые я использовал, показаны ниже с фрагментом кода.
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>();
}