我有两个集合:如何从动态数据构建JSON?
第一个是“父”节点:
例如
汽车
人民
公司
...
在每个的这些,还有一些数据。
- 汽车
1.1。本田
1.2。福特 - 人民
2.1。哈里森·福特
2.2。 Psy
2.3。杰西卡·奥尔芭 - 公司
3.1。 Oracle
3.2。微软
等等...
这些项目,我可以在我的C#应用程序遍历。
我怎么会去为了构建一个DataTable(或任何对象将工作最好与它)生产者模仿上面的树JSON?
这是我有什么,但它并没有真正给我什么,我需要:
public DataTable getNotificationTypeByUser(int id)
{
var bo = new HomeBO();
var list = bo.GetNotificationsForUser(id);
var notificationTreeNode = (from GBLNotifications n in list
where n.NotificationCount != 0
select new NotificationTreeNode(n)).ToList();
var table = new DataTable();
var column1 = new DataColumn
{
DataType = Type.GetType("System.String"),
ColumnName = "NotificationType"
};
table.Columns.Add(column1);
var column2 = new DataColumn
{
DataType = Type.GetType("System.String"),
ColumnName = "NotificationDescription"
};
table.Columns.Add(column2);
foreach (var node in notificationTreeNode)
{
var row1 = table.NewRow();
row1["NotificationType"] = node.ToString();
table.Rows.Add(row1);
var notifications = bo.GetNotificationsForUser(id, node.NotificationNode.NotificationTypeId);
foreach (GBLNotifications n in notifications)
{
var row = table.NewRow();
row["NotificationDescription"] = n.NotificationDescription;
table.Rows.Add(row);
}
}
return table;
}
这东西,我没有收藏却因为我不知道如何构建它正确 –
你的第一个林e说我有收藏品。无论如何,如果你没有用适当的字段创建所需的类,而不是添加到数据表中,你可以将它们添加到你的对象列表中 – Ehsan