我从菜单中创建了CSV
Main,Level 1,Level 2 ,Level 3,Level 4
Forest,,,,
,lvl1link,,,
,,lvl2link,,
,,lvl2link2,,
Cloud,,,,
,clv1,,,
,,clvl2,,
,,clvl22,,
,,,clvl3,
,,,,clvl4
Energy,,,,
,enlvl1,,,
,enlvl11,,,
,,enlvl2,,
,,,enlvl3,
,,,enlvl31,
我从另一个项目导出CSV,我需要创建子网站,并重新创建菜单结构。
如何将其转换为JSON,其中Forest
是lvl1link
的父级,lvl1link
是lvl2link
,lvl2link2
等的父级。
在我看来,它可以通过一些递归函数来完成。
示例步骤
- 检查具有
Main
元素值的前2行的索引,并记录其索引 - 现在,当查找col2中的元素时,请检查第二行的索引,并注意它们的索引
- 通过重复以上步骤构建多维数组,并返回以下内容
array(
'Forest' => array(
'lvl1link' => array(
'lvl2link',
'lvl2link2'
)
),
'Cloud' => array(
'clv1' => array(
'clvl2',
'clvl22' => array(
'clvl3' => array(
'clvl4'
)
)
)
),
'Energy' => array(
'enlvl1',
'enlvl11' => array(
'enlvl2' => array(
'enlvl3',
'enlvl31'
)
)
)
);
我可以使用PHP,Python或JavaScript。
请告诉我正确的方向
2条答案
按热度按时间o4tp2gmn1#
上下文和目标对我来说并不清楚,但是我会将CSV加载到数据库中,然后执行适当的查询以获得连接结果。例如,Talisman MPCS解决方案能够将csv加载到支持In-Memory SQL的数据存储中,并公开服务以自动生成JSON查询结果。
drkbr07n2#
使用递归遍历整个结构