-2

I need to have the below list of folders (directory listing) to be converted to JSON using Java:

"ListDirectories/"
"ListDirectories/folder1/"
"ListDirectories/folder1/folder3/"
"ListDirectories/folder2/"

And I want JSON as:

String data = "{text: 'ListDirectories',nodes:[{text:'folder1',nodes:[{text:'folder3'}]},{text:'folder2}]}";
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

1

You can also refer to this. Here its solving using jquery , but similar approach can be taken to solve using java.

0

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.

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.

segfault
  • 504
  • 2
  • 6
  • Thanks for the answer. I will try doing that. Sorry for my poor knowledge here, but what is creating file system tree here – srujana bhamidipalli Feb 12 '18 at 21:17
  • Sure! Usually, when you want to store/parse a file system, one of the approaches you could take is to create a tree which represents it. ListDirectories has two children folder1 and folder2, folder1 has one child : folder3 – segfault Feb 12 '18 at 21:29