简体   繁体   English

使用递归创建深度嵌套的JSON

[英]Create Deeply Nested JSON with Recursion

This JSON is the output that I am expecting, 这个JSON是我期望的输出,

    [{
    "text": "a",
    "nodes": [{
        "text": "aa",
        "nodes": [{
            "text": "aa1"
        }, {
            "text": "aa2",
            "nodes": [{
                    "text": "aaa1"
                },
                {
                    "text": "aaa2",
                    "nodes": [{
                        "text": "aaaa21"
                    }]
                },
                {
                    "text": "aaa3"
                }
            ]
        }]
    }]
},
{
    "text": "b",
    "nodes": [{
            "text": "bb1"
        },
        {
            "text": "bb2"
        }
    ]
},
{
    "text": "c",
    "nodes": [{
        "text": "cc"
    }]
},
{
    "text": "d",
    "nodes": [{
        "text": "dd1",
        "nodes": [{
                "text": "ddd1"
            },
            {
                "text": "ddd2"
            },
            {
                "text": "ddd3",
                "nodes": [{
                    "text": "ddd31",
                    "nodes": [{
                        "text": "ddd32"
                    }]
                }]
            }
        ]
    }]
}]

So to in short from the below table structure I want to generate a JSON tree like above, Please someone share me a java code/Algorithm for this.. 因此,简而言之,从下面的表结构中,我想生成一个像上面的JSON树,请为此共享一个Java代码/算法。

A simple recursion method would help me, Or Is there any standard library to do this 一个简单的递归方法会对我有帮助,或者是否有任何标准库可以做到这一点

在此处输入图片说明

Your immediate problem is that your recursive function executes the SQL query each time it is called and so never gets beyond the first result row. 您的直接问题是,递归函数每次都将执行SQL查询,因此永远不会超出第一个结果行。 Hence the stack overflow. 因此,堆栈溢出。 Re-structure your code so that the query is executed just once at the beginning. 重新组织代码,以便查询仅在开始时执行一次。 I haven't got time to set up a table to try this out, but intuitively, you need to move the function to start just before the 'while' loop. 我没有时间建立表来进行尝试,但是直觉上,您需要将函数移到“ while”循环之前开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM