Convert directory structure to JSON?

I found this code but it’s all Greek to me…can this be converted to work in Xojo?

It would make a nice alternative to the Hierarchical ListBox example in the Language Reference :slight_smile:

Not tested, but here you go:

Function DirectoryTree(Extends keyNode as FolderItem) as JSONItem dim ji as new JSONItem ji.Value("name") = keyNode.DisplayName ji.Value("path") = keyNode.NativePath if keyNode.Alias then ji.Value("type") = "alias" elseif keyNode.Directory then ji.Value("type") = "folder" dim k,q as Integer dim enfant as FolderItem dim jarr as new JSONItem q = keyNode.Count for k = 1 to q enfant = keyNode.TrueItem(k) //note you do NOT want to follow aliases here jarr.Append(enfant.DirectoryTree) next k ji.Value("children") = jarr else ji.Value("type") = "file" end if Return ji

Thanks Andrew,

What about making it more simple…say we do a normal recursive scan first and collect all the subfolders from a user selected folder. Then we strip the native paths so we get strings like:


Can we use JSON to structure the data and display it in a Hierarchical ListBox?

You don’t even need Json to do this. I’m using this sort of data myself to load this into a listbox. I’ll dig this out tomorrow.

Here it is:

The original code is from Matt Neuberg ca. 1999. I just modernized the code a bit.


Thanks Beatrix…I think I do remember it.

Wouldn’t json format be easier to save to disk…since it’s already structured and ready to write out?

It really depends on the data. The data I have in this form are mailboxes. The app needs to save the UI state - if the mailboxes are opened or closed. So I need a dictionary anyway. Most people have a couple of hundred mailboxes.

Here’s an example from doofus:

I’m wondering if this would be easier with JSON and without the need to create a class to store values etc.

Hello guys , any new updates on this issue ? i have same problem here and it seems that the example of J Andrew Lipscomb gives some errors related to .DirectoryTree missing and on the link of Beatrix Willius it says missing file.

I have a Directory structure like :

Files -> Directory 1 -> File1
-> File2
-> File3
-> Directory 2 -> File1
-> File2
-> File3
-> Directory 3 -> File1
-> File2
-> File3
-> Directory 4 -> File1
-> File2
-> File3
-> Directory 5 -> File1
-> File2
-> File3

and the json format has to be something like :

“USBfolders”:[{“FolderName”:“Directory1”,“Files”:[{“FileName”:“File1”},{“FileName”:“File2”},{“FileName”:“File3”}]},{“FolderName”:“Directory2”,“Files”:[{“FileName”:“File1”},{“FileName”:“File2”},{“FileName”:“File3”}]}, {“FolderName”:“Directory3”,“Files”:[{“FileName”:“File1”},{“FileName”:“File2”},{“FileName”:“File3”}]}, {“FolderName”:“Directory4”,“Files”:[{“FileName”:“File1”},{“FileName”:“File2”},{“FileName”:“File3”}]}, {“FolderName”:“Directory5”,“Files”:[{“FileName”:“File1”},{“FileName”:“File2”},{“FileName”:“File3”}]}]

and it seems that i dont get very well this JSON part , i manage to get the folders but now, on the file part im kind of stuck and i i have to see how i create that JSON structure

and the final structure should be something like :

{“syncFolders”:[], “USBfolders”:[]}

the sync folder i have already the structure from the api and i just have to send it back, but i`m stuck with the usb part .

Any ideas ?

Thanks in advance