我正在开发一个MVC应用程序,从一个结构如下的SQL Server表中检索数据:
+-----------------------------------+
| Id | Name | Hierarchy | Depth |
|-----------------------------------|
| 01 | Justin | / | 0 |
| 02 | Chris | /1 | 1 |
| 03 | Beth | /1/1 | 2 |
+-----------------------------------+
Hierarchy
列中的示例数据是hierarchyid
数据类型的字符串表示,Depth
列使用hierarchyid::GetLevel()
方法进行计算。
使用Entity Framework 4.1,我将上述表映射到以下类:
public class Node {
public int Id { get; set; }
public string Name { get; set; }
public string HierarchyPath { get; set; } // String representation of the hierarchyid
public int Depth { get; set; }
}
我希望使用这些信息,通过JS可视化工具包向用户展示层次结构的图形表示。该工具需要数据进行结构化处理:
var node = {
id: 1,
name: 'Justin'
children: [{
id: 2,
name: 'Chris',
children: [{
id: 3,
name: 'Beth',
children: []
}]
}]
}
我在开发中遇到了一个问题,就是如何将我的模型列表转换为结构化的JSON对象。有什么建议吗?
/2
。/2
的第一个子节点将具有路径/2/1
,然后是第二个子节点/2/2
,以此类推。更好的示例可以在此处找到:http://technet.microsoft.com/en-us/library/bb677195.aspx。 - Justin Rusbatch