我有兩個集合:如何從動態數據構建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