简体   繁体   English

使用 Java 将目录列表转为 JSON

[英]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}]}";

You can also refer to this .你也可以参考这个 Here its solving using jquery , but similar approach can be taken to solve using java.这里使用 jquery 解决,但可以采用类似的方法使用 java 解决。

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.

相关问题 使用Java在目录中列出文件时使用通配符 - Using wildcard when listing files in directory with java Java目录列表问题 - Java Directory Listing problem 尝试使用 Java 解析 HTML 目录列表中的链接 - Trying to parse links in an HTML directory listing using Java 在Java中按需列出目录中的文件 - Listing Files in a Directory On Demand in Java Java Picasa API目录列表不起作用 - Java Picasa API Directory listing not working 如何在Java中使用结果列表本身的属性来过滤目录列表? - How to filter directory listing by using a property from the resultant list itself in Java? 使用Java.io.File列出目录内容时出现奇怪的行为 - Strange behavior while listing directory contents using Java.io.File 在Java中,是否可以使用资源路径在WAR类路径中获取目录文件列表? - In Java is it possible to get a directory file listing inside a WAR classpath using a resource path? 使用java中的File.listFiles()在Linux环境中的项目目录中列出文件 - Listing files in project directory in Linux environment using File.listFiles() in java 列表中安装了Java,但执行产生“./java:没有这样的文件或目录” - Java is installed, in listing, but execution produces “./java: No such file or directory”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM