如何将我的所有数据(文件和文件夹)转换为 JSON 文件



使用应用程序脚本,我想知道如何转换我的所有数据,即我的所有文件和文件夹并将其转换为json文件。

从您的问题中不清楚您要对文件和文件夹做什么,因此,如果您想记录文件的名称,文件夹以 JSON 格式列出它们,那么以下代码将返回云端硬盘中文件和文件夹的名称。

var filesArr = [];
var foldersArr = [];
var filesFoldersObj = {};
function doGet(request){
var myJSON = getFilesFoldersToJSON();
// return JSON text with the appropriate Media Type
return ContentService.createTextOutput(myJSON).setMimeType(ContentService.MimeType.JSON);
}
function getFilesFoldersToJSON() {
// Get the name of every file in Drive.
var files = DriveApp.getFiles();
while (files.hasNext()) {
var file = files.next();
filesArr.push(file.getName());
}
// create json string of file names
var filesJSON = JSON.stringify(filesArr);
// Get the name of every folder in Drive.
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
foldersArr.push(folder.getName());
}
// create json string of folder names
var foldersJSON = JSON.stringify(foldersArr);
filesFoldersObj['files'] = filesArr;
filesFoldersObj['folders'] = foldersArr;
Logger.log(filesFoldersObj);
var myJSON = JSON.stringify(filesFoldersObj);
// log myJSON to check it - remove or comment out after checking...
Logger.log(myJSON);
return myJSON;
}

生成这样的 JSON...

{
"files": [
"Files and Folders to JSON",
"Test File 6",
"Test File 5",
"PDF File 6.pdf",
"PDF File 5.pdf",
"PDF File 4.pdf",
"PDF File 3.pdf",
"PDF File 2.pdf",
"PDF File 1.pdf",
"Test File 1",
"Test File 2",
"Test File 3",
"Test File 4",
"Getting started"
],
"folders": [
"Test Folder 6",
"Test Folder 5",
"Test Folder 2",
"Test Folder 1",
"Test Folder 4",
"Test Folder 3"
]
}

相关内容

  • 没有找到相关文章

最新更新