JSON 数据的棘手转换
我有主管给受主管的 json 数据。我需要将其放入嵌套形式才能使用 lib_ggOrgChart。问题是……太难了!努力寻找一种简单的方法来做到这一点。也许递归或者其他什么...有人有什么想法吗?
我知道我可以编写一些强力代码......但我希望有一个更优雅的解决方案。任何帮助表示感谢!
{
{"Supervisor_ID": 1, "Supervisee_ID": 2},
{"Supervisor_ID": 1, "Supervisee_ID": 3},
{"Supervisor_ID": 1, "Supervisee_ID": 4},
{"Supervisor_ID": 2, "Supervisee_ID": 5},
{"Supervisor_ID": 3, "Supervisee_ID": 6},
{"Supervisor_ID": 4, "Supervisee_ID": 7},
{"Supervisor_ID": 4, "Supervisee_ID": 8},
{"Supervisor_ID": 4, "Supervisee_ID": 9}
}
我需要把它变成这样的形式:
{
'root':{
'id': 1,
'children': {
{
'id': 2,
'children': {
{
'id': 5
}
}
},
{
'id': 3,
'children': {
{
'id': 6
}
}
},
{
'id': 4,
'children': {
{
'id': 7
},
{
'id': 8
},
{
'id': 9
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样就可以解决问题了。
关键步骤
创建 OrgTree
首先,使用您的输入创建一个 OrgTree,它实际上是一个
Map>
并将每个员工 ID 映射到员工 ID 集合,该集合是该人员员工ID属于主管。这是下一步的基础。
给定
n
个关系 (Supervisor => Supervise) 以及Set
和Map
的恒定查找时间,这需要O(n).
创建所需的对象结构
考虑函数
createOrgChartForEmployee()
,它为给定的 Employee 创建 OrgChart。在这里,我们首先构建 OrgTree,如上所述,然后从给定的员工开始,即查找他/她的孩子,然后调用
buildEmployee()
。这就是递归的开始。在
buildEmployee()
中,我们需要检查子项是否未定义,对于本身不监督任何员工的员工来说,确实会发生这种情况。在这种情况下,我们只返回员工的 ID。这是我们的基本情况并结束递归。现在,所有其他员工都将有员工进行监督,因此对所有子员工递归调用
buildEmployee()
并将每个buildEmployee()
调用的结果映射到包含所有员工的数组中孩子并返回该数组。有一些特殊的输入需要考虑:
退化为列表:
输入:
1 -> 2-> 3-> 4-> 5-> 6-> ...-> n
这将导致最大树深度为
n - 1
=O(n)
。无限循环
输入:
1 -> 2-> 3-> 1
像这样的输入将导致无限循环,并且迟早会出现错误
超出最大调用堆栈大小
。但对于 OrgCharts 来说,这个输入不应成为问题。This will do the trick.
Key steps
Create OrgTree
First using your input I am creating an OrgTree which is actually a
Map<number, Set<number>>
and maps each employee ID to the set of employee IDs, which the person the employee ID belongs to supervises.This is the foundation for the next step.
Given
n
relations (Supervisor => Supervisee) and constant lookup time forSet
andMap
this takesO(n)
.Creating desired object structure
Consider the function
createOrgChartForEmployee()
which creates the OrgChart for a given Employee.Here we first build the OrgTree, as described above, then start at the given employee i. e. lookup his/ her children and then call
buildEmployee()
.That's the start of the recursion. In
buildEmployee()
we need to check if children are undefined, this does happen for employees who themselves do not supervise any employees. In that case we just return the ID of the Employee. This is our base case and ends the recursion.Now, all other employees will have employees to supervise, so recursively call
buildEmployee()
on all children and map the result of everybuildEmployee()
call into an array that contains all children and return that array.There are some special inputs to consider:
Degeneration to list:
Input:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> ... -> n
This will result in a maximum tree depth of
n - 1
=O(n)
.Infinite Loop
Input:
1 -> 2 -> 3 -> 1
An input like this will result in an infinite loop and sooner or later in an error
Maximum call stack size exceeded
. But for OrgCharts this input should not be a concern.我不知道我是否正确理解了您想要实现的目标,因为我发现示例
我需要将其变成这种形式
有点令人困惑(似乎第一个主管出于某种原因包装了所有内容? )。无论如何,请尝试一下:I don't know if I properly understood what you're trying to achieve as I found the example
I need to get it into this form
kinda confusing (seems like first supervisor is wrapping all for some reason?). Anyways, take a shot at this: