我正在使用UrlFetch从Googe脚本中获取数据:
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
Logger.log(typeof(response))
这是记录:
[["Name","email","2016-12-31T00:00:00.000Z","Sim",548]]
object
响应看起来像一个数组,但它是一个对象。我需要能够遍历一个数组,所以我该如何将这些数据转换为数组呢?
答案 0 :(得分:1)
这取决于查询返回的内容类型。例如,假设它是JSON:
function myFunction() {
var url = "http://headers.jsontest.com/";
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
try {
// Regular expression for JSON content-type
var jsr = new RegExp(/application\/json/);
var cnt = response.getAllHeaders()["Content-Type"];
// Check if response is JSON
if (jsr.exec(cnt)) {
// Parse response content
var js = JSON.parse( response.getContentText() );
Logger.log(js);
for (var i in js) {
var t = {}; t[i] = js[i];
Logger.log( t );
}
}
} catch(e) {
Logger.log(e)
}
}