问题
I am looking to read a json file, namely config.json
from a AppScript to generate a Google Form from the meta data in the config.json
. I am not sure how to read the JSON file - could someone help me with it please? Some example code would be greatly appreciated.. Thanks
回答1:
Assuming that your file is in Google Drive, you can use the DriveApp service to read the file and parse it.
function getFileContent() {
var fileName = "config.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);
}
}
Make sure that Google Drive has not converted the text file into a native format else the script won't be able to parse it.
来源:https://stackoverflow.com/questions/53359704/load-or-read-a-json-from-local-in-google-appscript