日期:2014-05-20  浏览次数:21022 次

面试题:关于String
首先来看看数据库里的数据(这是一个菜单的数据,含多级):


要求从数据库里取出所有数据,并组合成以下格式:


组合的规则如下(JSON格式):
[{"ID":1,"PID":0,"TEXT":"首页","CHILDREN":[{"ID":2,"PID":1,"TEXT":"用户管理","CHILDREN":[{"ID":5,"PID":2,"TEXT":"用户列表"},{"ID":6,"PID":2,"TEXT":"添加用户"},{"ID":7,"PID":2,"TEXT":"修改密码"}]},{"ID":3,"PID":1,"TEXT":"资源管理","CHILDREN":[{"ID":8,"PID":3,"TEXT":"资源列表"},{"ID":9,"PID":3,"TEXT":"增加资源"}]},{"ID":4,"PID":1,"TEXT":"权限管理","CHILDREN":[{"ID":10,"PID":4,"TEXT":"权限列表"},{"ID":11,"PID":4,"TEXT":"增加权限"},{"ID":12,"PID":4,"TEXT":"删除权限"}]}]}]

格式化后的JSON格式:

[
    {
        "ID": 1,
        "PID": 0,
        "TEXT": "首页",
        "CHILDREN": [
            {
                "ID": 2,
                "PID": 1,
                "TEXT": "用户管理",
                "CHILDREN": [
                    {
                        "ID": 5,
                        "PID": 2,
                        "TEXT": "用户列表"
                    },
                    {
                        "ID": 6,
                        "PID": 2,
                        "TEXT": "添加用户"
                    },
                    {
                        "ID": 7,
                        "PID": 2,
                        "TEXT": "修改密码"
                    }
                ]
            },
            {