我有以下JSON,其中包含
我怎样才能从JSON中获取所有路径和文件,并且输出一个包含以下元素的数组?
files
和folders
(保留字),用于指示哪些是文件和文件夹。the_name_of_the_folder_itself:
"files": [array_of_files_]
"folders": [objects of folders inside this dir]
更容易理解JSON本身:
{
"configfiles": {
"folders": {
"dir1": {
"files": [
"data.dat",
"data2.dat"
]
},
"dir2": {
"folders": {
"subffolder": {
"files": [
[]
]
},
"subffolder_2": {
"files": [
"other.dat"
]
}
},
"files": [
[]
]
}
},
"files": [
[]
],
"LoadBase": "barfoo IGNORE THIS"
}
}
我怎样才能从JSON中获取所有路径和文件,并且输出一个包含以下元素的数组?
configfiles/
configfiles/dir1/
configfiles/dir1/data.dat
configfiles/dir1/data2.dat
configfiles/dir2/
configfiles/dir2/subffolder/
configfiles/dir2/subffolder_2/
configfiles/dir2/subffolder_2/other.dat
这是目前我所做的尝试:
function getPathAndFolder(folderPath)
{
foldersArray = Object.keys(folderPath)
foldersArray.forEach(function callback(folderName, index, array)
{
finalArray.push(folderName)
filesArray = folderPath[folderName].files
filesArray.forEach(function callback(fileName, index, array)
{
finalPath = folderName + "/" + fileName
});
// Call it again
//getPathAndFolder()
});
}
finalArray = []
getPathAndFolder(inputJSONobject)
有人能帮帮我吗?
{folders: { configfiles: { /* ... */ } }
吗? - yunzen文件夹
。 - Avión