从嵌套集生成JSON(perl,sql,jquery)

时间:2011-02-03 09:22:23

标签: sql json jstree nested-sets

我在数据库中有内容页面(使用嵌套集),我需要通过jQuery jsTree插件显示它。需要使用以下数据返回JSON:

[
    {
        data: 'node1Title',
        children: [
            {
                data: 'subNode1Title',
                children: [...]
            },
            {
                data: 'subNode2Title',
                children: [...]
            }
        ]
    },
    {
        data: 'node2Title',
        children: [...]
    }
]

我需要做什么?

我可以将哈希数组转换为JSON,但我不明白如何生成数组。

示例数据:

**'pages'table**
id  parent_id   level   lkey    rkey    name
1   0       1   1   14  index
2   1       2   2   7   info
3   1       2   8   13  test
4   2       3   3   4   about
5   2       3   5   6   help
6   3       3   9   10  test1
7   3       3   11  12  test2

我需要得到:

[
    {
        data: 'index',
        children: [
            {
                data: 'info',
                children: [
                    {
                        data: 'about'
                    },
                    {
                        data: 'help',
                    }
                ]
            },
            {
                data: 'test',
                children: [
                    {
                        data: 'test1'
                    },
                    {
                        data: 'test2'
                    }
                ]
            }
        ]
    }
]

4 个答案:

答案 0 :(得分:2)

我有完全相同的问题,这是我在Perl中编写的将我的嵌套集树转换为jsTree插件的JSON对象(我使用DBIx :: Tree :: NestedSet来访问MySQL数据库树)。从Perl的角度来看,我知道我的代码是丑陋的,但它对我有用。

sub get_json_tree {
    my $json = '[';
    my $first = 1;
    my $last_level = 1;
    my $level = 1;
    my $tree = DBIx::Tree::NestedSet->new(dbh => $dbh);
    my $ancestors = $tree->get_self_and_children_flat(id => $tree->get_root);
    foreach (@{$ancestors}) {
        my $name = $_->{'name'};
        $last_level = $level;
        $level = $_->{'level'};
        if ($level > $last_level) {
            $json .= ',' if ($json =~ /}$/);
        } elsif ($level < $last_level) {
            $json .= ']}';
            for (my $i = 0; $i < $last_level - $level; $i++) {
                $json .= ']}';
            }
            $json .= ',';
        } elsif ($level == $last_level && !$first) {
            $json .= ']},';
        }
        $json .= '{"attr":{"id":'.$_->{'id'}.',"rel":"folder"},"data":"'.$name.'","children":[';
        $first = 0;
    }
    $json .= ']}';
    for (my $i = 1; $i < $level; $i++) {
        $json .= ']}';
    }    
    $json .= ']';
    return $json;
}

答案 1 :(得分:1)

我正在寻找它。也许DataTable插件示例提供了一个解决方案。我正在查看插件目录/examples/server_side/scripts/ssp.class.php。你可以download it here

看看在#34;服务器端脚本&#34;中使用它的最简单方法。 this documentation中的标签。

答案 2 :(得分:1)

这很简单。你需要编写一个递归函数。我用Perl写的。 $ list - 这是你的数组按&#39; left_key&#39;。

排序
sub make_tree {
    my $list = shift;
    my @nodes;

    while (my $node = shift @$list) {
        if (@$list and $node->{level} < $list->[0]{level}) {
            $node->{data} = make_tree($list);
            push @nodes, $node;
        }
        last if @$list and $node->{level} > $list->[0]{level};
    }

    return \@nodes;
}

my $hash = make_tree($list);

答案 3 :(得分:0)

最近我一直在寻找类似的解决方案。在发布我自己的问题之后,我才发现这一点。我发布的最终代码我认为可以很好地回答你的问题。

我使用以下代码和DBIx::Tree::NestedSet的修改版本。我使用此代码创建嵌套集树的JSON输出。

Convert a flat datastructure into a tree

sub get_jsonTree {
    my ($array_of_hashes_ref) = @_;

    my $roots;
    my %recs_by_name;
    my %children_by_parent_name;
    my %count;
    for my $row (@$array_of_hashes_ref) {
        my $name        = $row->{position_id};
        my $parent_name = $row->{placement_id};

        my $rec = {
            name => $name,
        };

        ## Added to loop through all key,value pairs and add them to $rec
        while ( my ($key, $value) = each(%$row) ) {
            $rec->{$key} = $value;
        }

        ##Added To Count Child Nodes
        $count{$parent_name} = 0 if (!$count{$parent_name});        
        $rec->{'child_count'} = $count{$parent_name};
        $count{$parent_name}++;

        push @{ $children_by_parent_name{$parent_name // 'root'} }, $rec;
        $recs_by_name{$name} = $rec;
    }

    $roots = delete($children_by_parent_name{root}) || [];

    for my $name (keys(%children_by_parent_name)) {
        my $children = $children_by_parent_name{$name};
        if ( my $rec = $recs_by_name{$name} ) {
            $rec->{children} = $children;
        } else {
            $util{'test'} .=  "Parent $name doesn't exist.\n<BR>";
            push @$roots, @$children;
        }
    }

    use JSON;
    my $json_str = encode_json(@{$roots}[0]);

    return $json_str;
}

my $array_of_hashes_ref = [
    {  position_id => 123, placement_id => undef },
    {  position_id => 456, placement_id => 123 },
    {  position_id => 789, placement_id => 123 },
    # ...
];

my $json_str = &get_jsonTree($array_of_hashes_ref);