我有一个散列数组。数组中的每个元素都是分层树中的一个节点,并且具有父代身份的参考数据。我将在树中有成千上万的节点......基本上,一组未知的节点必须转换为JSON(如下所示),以便与http://bl.ocks.org/robschmuecker/7880033将平面数据结构转换为树
一起使用更新:position_id是异端树中的节点。 placement_id是父级的position_id(邻接参考树)。
更新:下面是完整的AoH Data :: Dumper结果,其中嵌套集和邻接结果来自DBIx::Tree::NestedSet(自定义)的修改版本。
$VAR1 = [
{
'lft' => '673',
'id' => '109',
'date_created' => '2015-08-15',
'level' => '7',
'user_id' => '13',
'placement_id' => '11',
'position_id' => '13',
'status' => '1',
'structure_id' => '1',
'rght' => '684'
},
{
'placement_id' => '13',
'position_id' => '22',
'status' => '1',
'structure_id' => '1',
'rght' => '679',
'lft' => '674',
'date_created' => '2015-08-15',
'id' => '116',
'level' => '8',
'user_id' => '22'
},
{
'user_id' => '101',
'level' => '9',
'id' => '200',
'date_created' => '2015-08-15',
'lft' => '675',
'rght' => '676',
'structure_id' => '1',
'status' => '1',
'position_id' => '101',
'placement_id' => '22'
},
{
'date_created' => '2015-08-15',
'id' => '201',
'level' => '9',
'user_id' => '374',
'lft' => '677',
'structure_id' => '1',
'rght' => '678',
'placement_id' => '22',
'position_id' => '374',
'status' => '1'
},
{
'lft' => '680',
'user_id' => '95',
'level' => '8',
'id' => '117',
'date_created' => '2015-08-15',
'status' => '1',
'position_id' => '95',
'placement_id' => '13',
'rght' => '681',
'structure_id' => '1'
}
];
这是我们的目标,在这个例子中,我需要结束:
{
"name": "13",
"children": [
{
"name": "22",
"children": [
{
"name": "101"
},
{
"name": "374"
}
]
},
{
"name": "95"
}
]
}
你还可以看到我想来到这里的格式(减尺寸): http://bl.ocks.org/robschmuecker/7880033#flare.json
我的失败方法包括循环遍历哈希数组的各种尝试,以创建一个递归哈希散列,然后可以与JSON Perl module一起使用来创建我需要的实际JSON。
可能相关:http://stackoverflow.com/questions/31108400/perl-two-column-flat-file-to-a-complex-unordered-list-tree – melpomene
如果第一个哈希有1级而不是7级,它会是什么样子? –
它看起来是一样的。关卡本身就是更大树内的整体关卡。所以,13是整个树的第7层,但是这个组的第1层。 – RedSands