如何使用Google Apps脚本从Google云端硬盘检索JSON特定的内容? [英] How to retrieve JSON specific content from Google Drive with Google Apps Script?

查看:66
本文介绍了如何使用Google Apps脚本从Google云端硬盘检索JSON特定的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件夹树:

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 如下所示:

The JSON file document.json.txt for the tree above, which is uploaded to my Google Drive, is like so:

[
  {"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":[
      ]}
    ]}
  ]}
]

以下脚本(加载或从Google AppScript中的本地读取JSON )可以很好地从Google云端硬盘检索上述JSON文件的全部内容:

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:

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

How shoud I modify the script to get the contents of bbb 333 directory only?

推荐答案

  • 您要从 document.json文件中检索名称为 bbb 333 name 的对象中 contents 的值.txt 是JSON对象.
    • 您要检索以下值.
    • [[{{type:" directory," name:" yyy 111," contents:[]},{" type:" directory," name:" yyy 222,"目录:[]},{"类型:"目录,"名称:" yyy 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.
        • 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":[]}]]
        • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

          If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

          • 在此答案中,将遍历检索到的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
            }
          }
          

          结果:

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

          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.

          这篇关于如何使用Google Apps脚本从Google云端硬盘检索JSON特定的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆