I would like to know if there is a way to get a specific directory structure and parse it to json so I can create a client-side treeview schema using a jquery plugin. thanks in advance
Asked
Active
Viewed 3,517 times
1
-
2What have you tried? Are you trying to get a directory from the client machine or somewhere from the server? – John Koerner Apr 03 '13 at 17:05
-
from the server, i want to show some files stored on the server. Right now I'm using Directory class, but I don't know how to handle the result to send it to the client. – mike3423 Apr 03 '13 at 17:08
3 Answers
6
Using JSON.NET:
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()
});
}
Example usage:
var json = GetDirectory(new DirectoryInfo("...some path...")).ToString();
This will give you JSON that looks something like this:
{
"directory":
{
"dirA": {
"file" : [ "file0.txt", "file1.jpg" ]
},
"emptyDir": {
}
},
"file": [ "file2.png" ]
}

Timothy Shields
- 75,459
- 18
- 120
- 173
-
it does, but also throws a lot of "\r\n", do you know which could be the reason? – mike3423 Apr 03 '13 at 18:27
-
"throws '\r\n'"? What do you mean? The default JSON.NET serializer will add formatting and newlines, yes. You can disable that if you want. – Timothy Shields Apr 03 '13 at 18:41
-
I'm using firebug and JSONLint to check the results, the response of the call comes with those characters and JSONLint says its invalid, but if I use the console printed response, it says is valid. – mike3423 Apr 03 '13 at 18:51
1
In fact, there is an easy way to convert a C# object to JSON using Json.NET.
You simply create a List<>
that contains the data you want and then call
var wrapper = new { TreeData= list };
string json = JsonConvert.SerializeObject(wrapper);

Nikolay Kostov
- 16,433
- 23
- 85
- 123

jugg1es
- 1,560
- 18
- 33
-
I understand how to parse an object to Json, but in this case, if I try to directly parse my Directory.EnumerateDirectories(directoryPath) to Json, it doesn't give a correct Json object, so I think the main problem is how to obtain the correct structure of the list before to parse it. – mike3423 Apr 03 '13 at 17:18
-
You will have to generate it yourself. Create a list of objects like `List
` and give it some properties like `DirectoryName` and a list of its own children `List – jugg1es Apr 03 '13 at 17:40`, then iterate recursively through the directory tree and fill your list. Try parsing that into JSON
-1
You can create custom classes like:
abstract class DirectoryChildItem
{
public string Name { get; set; }
}
class Directory : DirectoryChildItem
{
public List<DirectoryChildItem> Childs { get; set; }
}
class File : DirectoryChildItem
{
}
Then you should traverse your file system using static class System.IO.Directory and create items using classes above. After traversing file system use "return Json(obj)" method in your ASP.NET MVC action

Nikolay Kostov
- 16,433
- 23
- 85
- 123