【发布时间】:2016-03-04 12:45:08
【问题描述】:
我正在尝试解析从 API 调用中收到的 JSON,但我一直遇到错误 "TypeError: Cannot read property "id" from undefined. (line 42, file "")" em> 我对 Apps 脚本比较陌生。关于发生了什么的任何想法?我可以以 JSON 格式获取有效负载,但似乎无法解析它。
function getInfo() {
var url = "https://subdomain.chargify.com/subscriptions.json";
var username = "xxx"
var password = "x"
var headers = {
"Authorization": "Basic " + Utilities.base64Encode(username + ':' + password)
};
var options = {
"method": "GET",
"contentType": "application/json",
"headers": headers
};
var response = UrlFetchApp.fetch(url, options);
var data = JSON.parse(response.getContentText());
Logger.log(data);
var id = data.subscription; // kicks back an error
// var id = data; works and returns the whole JSON payload
var ss = SpreadsheetApp.getActiveSheet()
var targetCell = ss.setActiveSelection("A1");
targetCell.setValue(id);
}
【问题讨论】:
-
您对
data的记录显示了什么?显然结构的顶层没有subscription键。 -
@Teemu,在 GAS 中,它是同步的。
标签: javascript json parsing google-apps-script