【问题标题】:How to retrieve JSON specific content from Google Drive with Google Apps Script?如何使用 Google Apps 脚本从 Google Drive 中检索 JSON 特定内容?
【发布时间】:2019-12-13 15:26:53
【问题描述】:

我有以下文件夹树:

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

上面树的 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":[
      ]}
    ]}
  ]}
]

以下脚本 (Load or Read a JSON from local in Google AppScript) 可以很好地从 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);
  }
}

如何修改脚本只获取bbb 333目录的内容?

【问题讨论】:

    标签: json google-apps-script


    【解决方案1】:
    • 您想从document.json.txt 的文件中检索namebbb 333 对象中contents 的值,该文件是JSON 对象。
      • 您要检索以下值。
      • [[{"type":"directory","name":"yyy 111","contents":[]},{"type":"directory","name":"yyy 222","contents":[]},{"type":"directory","name":"yyy 333","contents":[]}]]
    • 您希望通过修改您的 Google Apps Script 脚本来实现此目的。

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    修改点:

    • 在此答案中,将遍历检索到的 JSON 对象并检索预期值。为此,我添加了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
      }
    }
    

    结果:

    运行上述脚本时,可以在日志中看到以下结果。

    [
      [
        {
          "type": "directory",
          "name": "yyy 111",
          "contents": []
        },
        {
          "type": "directory",
          "name": "yyy 222",
          "contents": []
        },
        {
          "type": "directory",
          "name": "yyy 333",
          "contents": []
        }
      ]
    ]
    

    如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 2015-07-10
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多