[英]How to retrieve JSON specific content from Google Drive with Google Apps Script?
I have the following folder tree:我有以下文件夹树:
base
├── aaa
│ ├── aaa 111
│ ├── aaa 222
│ └── aaa 333
├── bbb
│ ├── bbb 111
│ ├── bbb 222
│ └── bbb 333
│ ├── yyy 111
│ ├── yyy 222
│ └── yyy 333
└── ccc
├── ccc 111
├── ccc 222
└── ccc 333
The JSON file document.json.txt for the tree above, which is uploaded to my Google Drive, is like so:上面树的 JSON 文件document.json.txt已上传到我的 Google Drive,如下所示:
[
{"type":"directory","name":"BASE","contents":[
{"type":"directory","name":"AAA","contents":[
{"type":"directory","name":"aaa 111","contents":[
]},
{"type":"directory","name":"aaa 222","contents":[
]},
{"type":"directory","name":"aaa 333","contents":[
]}
]},
{"type":"directory","name":"BBB","contents":[
{"type":"directory","name":"bbb 111","contents":[
]},
{"type":"directory","name":"bbb 222","contents":[
]},
{"type":"directory","name":"bbb 333","contents":[
{"type":"directory","name":"yyy 111","contents":[
]},
{"type":"directory","name":"yyy 222","contents":[
]},
{"type":"directory","name":"yyy 333","contents":[
]}
]}
]},
{"type":"directory","name":"CCC","contents":[
{"type":"directory","name":"ccc 111","contents":[
]},
{"type":"directory","name":"ccc 222","contents":[
]},
{"type":"directory","name":"ccc 333","contents":[
]}
]}
]}
]
The following script ( Load or Read a JSON from local in Google AppScript ) works fine to retrieve the whole contents of the above JSON file from Google Drive:以下脚本( 在 Google AppScript 中从本地加载或读取 JSON )可以很好地从 Google Drive 检索上述 JSON 文件的全部内容:
function getFileContent() {
var fileName = "document.json.txt";
var files = DriveApp.getFilesByName(fileName);
if (files.hasNext()) {
var file = files.next();
var content = file.getBlob().getDataAsString();
var json = JSON.parse(content);
Logger.log(json);
}
}
How shoud I modify the script to get the contents of bbb 333 directory only?我应该如何修改脚本以仅获取bbb 333目录的内容?
contents
in the object with name
of bbb 333
from the file of document.json.txt
which is the JSON object.您想从 JSON 对象的document.json.txt
文件中检索name
为bbb 333
的对象中contents
的值。
[[{"type":"directory","name":"yyy 111","contents":[]},{"type":"directory","name":"yyy 222","contents":[]},{"type":"directory","name":"yyy 333","contents":[]}]]
If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。
getContents
.为此,我添加了getContents
的功能。function getFileContent() {
// I added below function
var getContents = function getContents(data, contents) {
for (var key in data) {
if (data[key].name === "bbb 333") contents.push(data[key].contents);
if (typeof data[key] === "object") getContents(data[key], contents);
}
return contents;
}
var fileName = "document.json.txt";
var files = DriveApp.getFilesByName(fileName);
if (files.hasNext()) {
var file = files.next();
var content = file.getBlob().getDataAsString();
var json = JSON.parse(content);
// Logger.log(json);
var res = getContents(json, []); // Added
Logger.log(res) // Added
}
}
When above script is run, the following result can be seen at the log.当上面的脚本运行时,可以在日志中看到以下结果。
[
[
{
"type": "directory",
"name": "yyy 111",
"contents": []
},
{
"type": "directory",
"name": "yyy 222",
"contents": []
},
{
"type": "directory",
"name": "yyy 333",
"contents": []
}
]
]
If I misunderstood your question and this was not the result you want, I apologize.如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.