I want to get the directory structure on server and parse it to json format so that I can send this to jQuery treeview plugin(Dynatree) through ajax. Dynatree accepts tree data something like this:
[
{"title": "Item 1"},
{"title": "Folder 2", "isFolder": true, "key": "folder2",
"children": [
{"title": "Sub-item 2.1"},
{"title": "Sub-item 2.2"}
]
},
{"title": "Folder 3", "isFolder": true, "key": "folder3",
"children": [
{"title": "Sub-item 3.1"},
{"title": "Sub-item 3.2"}
]
},
{"title": "Lazy Folder 4", "isFolder": true, "isLazy": true, "key": "folder4"},
{"title": "Item 5"}
]
In this Question @Timothy Shields shown a way to get Directory from DirectryInfo class and parse the structure to Json format like this:
JToken GetDirectory(DirectoryInfo directory)
{
return JToken.FromObject(new
{
directory = directory.EnumerateDirectories()
.ToDictionary(x => x.Name, x => GetDirectory(x)),
file = directory.EnumerateFiles().Select(x => x.Name).ToList()
});
}
but the output is not like the above one and I don't know how can I manipulate it. I would appreciate if someone tell how I can produce above output from directory structure. Thanks.
Edit:
I have edited the question. I think the question is clear now.