【问题标题】:Drive Batch API returning text content in addition to json-parsable stuffDrive Batch API 返回文本内容以及可解析的 json 内容
【发布时间】:2019-10-02 22:48:22
【问题描述】:

我的批处理(创建一些文件夹)运行良好,但我得到的响应不是我期望的干净的 JSON,但它散布着纯文本标题。如何摆脱它们或正确解析它?

--batch__AAPXnCR1-5Q
Content-Type: application/http
Content-ID: response-1

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Wed, 02 Oct 2019 22:44:16 GMT
Expires: Wed, 02 Oct 2019 22:44:16 GMT
Cache-Control: private, max-age=0
Content-Length: 140

{
 "kind": "drive#file",
 "id": "1pznPt",
 "name": "SF1-B1",
 "mimeType": "application/vnd.google-apps.folder"
}

--batch_z3tbQ5Q

我只需要能够在创建这些文件夹后提取它们的 id 和 nams。

【问题讨论】:

  • 这就是你得到的响应来解析它。

标签: javascript google-apps-script google-api google-drive-api


【解决方案1】:

这个答案怎么样?

很遗憾,使用 Google Apps Script 和 Javascript 的内置函数无法直接解析响应值。因此,就我而言,我使用以下脚本解析响应值。我认为这种情况有几种方法。因此,请将此视为几个答案之一。

示例脚本 1:

var response = UrlFetchApp.fetch(url, options).getContentText(); // Here, the batch request is run.

var temp = response.split("--batch");
var parsedValue = temp.slice(1, temp.length - 1).map(function(e){return JSON.parse(e.match(/{[\S\s]+}/g)[0])});

示例脚本 2:

var response = UrlFetchApp.fetch(url, options).getContentText(); // Here, the batch request is run.

var parsedValue = response.match(/{[\s\S]+?}/g).map(function(e) {return JSON.parse(e)});

如果这不是您想要的方向,我深表歉意。

【讨论】:

  • 谢谢,我试试这个。顺便说一句,我使用的基本代码来自你的 github,非常有帮助!
  • @J. G. 感谢您的回复和使用。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
相关资源
最近更新 更多