【发布时间】:2020-04-11 16:03:57
【问题描述】:
我正在向一个成功的 API 发出请求,但我需要获取返回的数组的数据,下面我将介绍数组的外观,以便您帮助我提取数据
{ total_grand: 30600000,
total_billable: null,
total_currencies: [ { currency: null, amount: null } ],
total_count: 5,
per_page: 50,
data:
[ { id: 13998122,
pid: 1570982183,
tid: null,
uid: 5386231,
description: 'Finish the first part of the RCP mockup',
start: '2020-03-26T13:00:00-04:00',
end: '2020-03-26T16:00:00-04:00',
updated: '2020-04-02T13:25:15-04:00',
dur: 10800000,
user: 'Jose',
use_stop: true,
client: 'PLA',
project: 'Training',
project_color: '0',
project_hex_color: '#3750b5',
task: null,
billable: null,
is_billable: false,
cur: null,
tags: []
} ]
}
我想访问用户、项目、标签、客户端、开始、结束和描述,以便我可以将其放在我的电子表格中。我该怎么做?
这就是我执行请求的方式以及我尝试访问变量togglData中的数组中的数据的方式@
for (var i = 0; i < projects.length; i++) {
var listProjects = projects[i];
var reportURL = baseURL + '/reports/api/v2/details' + params;
var reportFetch = UrlFetchApp.fetch(reportURL, options);
var togglReport = JSON.parse(reportFetch.getContentText());
var togglData = togglReport["data"]["user"];
Logger.log(togglReport);
}
【问题讨论】:
-
1. 改为提供
console.log(togglReport)或console.log(JSON.stringify(togglReport))。记录器不提供准确的日志(=不是有效的 json;应引用字符串)。 2. 确保你的 json 是有效的。您可以通过更改而不是添加...来隐藏敏感细节。 3. 您是否尝试过将数据塑造成二维数组,即 Google 表格的setValues要求的格式? -
例如见this question。
-
是什么让你这么想?
console.log多年来一直在应用程序脚本中得到支持。 -
因为
console.log在我输入脚本时从未打印任何内容,所以我不知道是否需要做其他事情才能使其正常工作,这就是为什么当我在 GAS 中编程时我避免在我的脚本中使用console.log并使用Logger.log而不是@TheMaster -
@TheMaster 我只有一个月的 GAS 编程,我对这方面很陌生,你能看到我更新的问题吗?
标签: javascript google-apps-script