简体   繁体   English

如何使用 Google Apps Script 从 Google Drive 检索 JSON 特定内容?

[英]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目录的内容?

  • You want to retrieve the value of 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文件中检索namebbb 333的对象中contents的值。
    • You want to retrieve the following values.您想要检索以下值。
    • [[{"type":"directory","name":"yyy 111","contents":[]},{"type":"directory","name":"yyy 222","contents":[]},{"type":"directory","name":"yyy 333","contents":[]}]]
  • You want to achieve this by modifying your script of Google Apps Script.您希望通过修改 Google Apps 脚本的脚本来实现此目的。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Modification points:改装要点:

  • In this answer, the retrieved JSON object is traversed and the expected values are retrieved.在此答案中,遍历检索到的 JSON 对象并检索预期值。 For this, I added the function of getContents .为此,我添加了getContents的功能。

Modified script:修改后的脚本:

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
  }
}

Result:结果:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM