I am trying to create a JSON of a machine's directory structure given an array of all its files and paths.
The array looks something like this:
string[] dirArray = {
"./proc/15/task/15/exe",
"./proc/15/task/15/mounts/mounts.xml",
"./proc/15/task/15/mountinfo/mountinfo.xml",
"./proc/15/task/15/clear_refs/clear_ref.xml",
"./proc/14/loginuid/loginuid.xml",
"./proc/14/sessionid/sessionid.xml",
"./proc/14/coredump_filter/coredump_filter.xml",
"./proc/14/io/io.xml"
}
The target JSON I am aiming for is something like this:
{
".":
{
"file":
{
"name":"fileInRoot.xml"
},
"proc":
{
"file":
{
"name":"fileInProc.xml"
},
"15":
{
"file":
{
"name":"fileIn15.xml"
},
"task":
{
"file":
{
"name":"fileInTask.xml"
},
"15":
{
"file":
{
"name":"fileInTask.xml"
},
"mounts":
{
"file":
{
"name":"fileInMounts.xml"
}
},
"mountsInfo":
{
"file":
{
"name":"fileInMountsInfo.xml"
}
},
"clear_refs":
{
"file":
{
"name":"fileInClear_Refs.xml"
}
}
}
}
},
"14":
{
"file":
{
"name":"fileIn14.xml"
},
"task":
{
"file":
{
"name":"fileInTask.xml"
},
"loginUid":
{
"file":
{
"name":"fileInloginUid.xml"
}
},
"sessionid":
{
"file":
{
"name":"fileInsessionid.xml"
}
},
"coreDump_filter":
{
"file":
{
"name":"fileIncoreDump_filter.xml"
}
},
"io":
{
"file":
{
"name":"fileInIo.xml"
}
}
}
}
}
}
}
I want to create a JSON file that allows consumer component of the JSON to navigate through this directory structure. I have been trying to use the Directory, File and Path classes but perhaps the best way is to use the java.serializor(?) class to construct the JSON as I loop through the array, parsing its directories and files as i go?
dirArray. Can I assume there will always be exactly one file per directory? If not, then the JSON will need to change (you would need an array for the files in the JSON at each directory level). Also, can I assume there will be no directory called "file"? If any directory can be called "file", then you will need to change the JSON to account for that as well since there can't be two keys with the same name at the same level in the JSON.