[英]Directory listing to JSON using Java
I need to have the below list of folders (directory listing) to be converted to JSON using Java:我需要使用 Java 将以下文件夹列表(目录列表)转换为 JSON:
"ListDirectories/"
"ListDirectories/folder1/"
"ListDirectories/folder1/folder3/"
"ListDirectories/folder2/"
And I want JSON as:我希望 JSON 为:
String data = "{text: 'ListDirectories',nodes:[{text:'folder1',nodes:[{text:'folder3'}]},{text:'folder2}]}";
The way I would go about this would be to create a file system 'tree' by parsing the inputs and then converting this tree to a json.我要解决的方法是通过解析输入然后将此树转换为 json 来创建文件系统“树”。
A tree would have nodes as :一棵树的节点如下:
Node {
String name;
List<Node> children;
}
Try to form this tree when parsing the inputs.在解析输入时尝试形成这棵树。 When a node is not there, create it in the tree.
当节点不存在时,在树中创建它。
Implement the toJSON(tree) by converting the tree to a JSON recursively.通过将树递归地转换为 JSON 来实现 toJSON(tree)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.