我正在开发一个 MVC 应用程序,它从 SQL Server 中的表中检索数据,其结构如下:
+-----------------------------------+
| Id | Name | Hierarchy | Depth |
|-----------------------------------|
| 01 | Justin | / | 0 |
| 02 | Chris | /1 | 1 |
| 03 | Beth | /1/1 | 2 |
+-----------------------------------+
中的示例数据Hierarchy
列是字符串表示形式hierarchyid
数据类型,以及Depth
列的计算使用hierarchyid::GetLevel()
method.
使用 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 可视化工具包 http://thejit.org/,这需要对数据进行结构化:
var node = {
id: 1,
name: 'Justin'
children: [{
id: 2,
name: 'Chris',
children: [{
id: 3,
name: 'Beth',
children: []
}]
}]
}
我在开发将模型列表转换为结构化 JSON 对象的逻辑时遇到问题。有什么建议么?