【发布时间】:2015-07-03 15:59:07
【问题描述】:
我想从以下数据生成树结构:
[
{"first_name" => "Test", "id" => "1", "parent_id" => ""},
{"first_name" => "Test1", "id" => "2", "parent_id" => "1"},
{"first_name" => "Test2", "id" => "3", "parent_id" => "1"},
{"first_name" => "Test3", "id" => "4", "parent_id" => "2"}
]
我想创建 ruby 脚本来构建以下结构:
[
{
"first_name" => "Test",
"id" => "1",
"children" => [
{
"first_name" => "Test1",
"id" => "2",
"children" => [
{
"first_name" => "Test3",
"id" => "4"
}
]
},
{
"first_name" => "Test2",
"id" => "3"
}
}
]
在这种情况下你有什么建议吗?
【问题讨论】: