我正在使用脚本将JSON数据导入Google表格。但是JSON数据在数组中有数组。首先是“承诺”,然后是“交易”。
{"pledges":[{"pledgeId":1225981,"organizationId":358,"projectId":1170,"promotionId":null,"paymentMethod":"funds_transfer","isRecurrent":false,"pledgedAmount":{"cents":40000,"currency":"CZK"},"pledgedAt":"2017-09-06T20:58:19+02:00","donor":{"firstName":"XXXXXX","lastName":"XXXXXX","email":"XXXXXX","address":{"street":"XXXXXX","city":"XXXXXX","postCode":"XXXXXX"},"phone":"XXXXXX"},"wantDonationCertificate":false,"customFields":{"obraz_kalenadar":false},"transactions":[{"transactionId":1252314,"state":"sent_to_organization","sentAmount":{"cents":40000,"currency":"CZK"},"receivedAt":"2017-09-07T00:00:00+02:00","receivedAmount":{"cents":40000,"currency":"CZK"},"outgoingAmount":{"cents":39200,"currency":"CZK"},"outgoingVs":"XXXXXX","outgoingBankAccount":"XXXXXX"}]}
我已经开始使用不同的脚本并根据我的需要重写它。我能够检索除事务之外的所有JSON数据 - 这些字段返回“undefined”。我很确定,问题在于:承诺[i] [“交易”] [“transactionId”],但我无法弄清楚,我怎样才能更深入地了解那些数据正确。
function darujmeJSON() {
// URL pro Darujme API
var root = 'https://www.darujme.cz/api/v1/organization/358/pledges-by-filter?apiId=XXXXXX&apiSecret=XXXXXX&fromPledgedDate=';
// Set date X months back
var mesic = 4; //how many months
var now = new Date();
now.setMonth(now.getMonth()-mesic);
var endpoint = Utilities.formatDate(now, 'CET', 'yyyy-MM-dd');
// parameters for url fetch
var params = {
'method': 'GET',
'muteHttpExceptions': true,
};
try {
// call the Darujme API
var response = UrlFetchApp.fetch(root+endpoint, params);
var data = response.getContentText();
var json = JSON.parse(data);
// get pledges data
var pledges = json['pledges'];
// blank array to hold the pledges data for Sheet
var pledgesData = [];
// Add the pledges data to the array
for (var i = 0; i < pledges.length; i++) {
// put the pledges data into a double array for Google Sheets
if (pledges[i]["pledgeId"] != 0) {
pledgesData.push([
pledges[i]["pledgeId"],
pledges[i]["organizationId"],
pledges[i]["projectId"],
pledges[i]["promotionId"],
pledges[i]["paymentMethod"],
pledges[i]["isRecurrent"],
pledges[i]["pledgedAmount"]["cents"],
pledges[i]["pledgedAmount"]["currency"],
pledges[i]["pledgedAt"],
pledges[i]["donor"]["email"],
pledges[i]["donor"]["firstName"],
pledges[i]["donor"]["lastName"],
pledges[i]["donor"]["phone"],
pledges[i]["donor"]["address"]["street"],
pledges[i]["donor"]["address"]["city"],
pledges[i]["donor"]["address"]["postCode"],
pledges[i]["wantDonationCertificate"],
pledges[i]["customFields"]["obraz_kalenadar"],
pledges[i]["transactions"]["transactionId"],
pledges[i]["transactions"]["state"],
pledges[i]["transactions"]["sentAmount"]["cents"],
pledges[i]["transactions"]["sentAmount"]["currency"],
pledges[i]["transactions"]["receivedAt"],
pledges[i]["transactions"]["receivedAmount"]["cents"],
pledges[i]["transactions"]["receivedAmount"]["currency"],
pledges[i]["transactions"]["outgoingAmount"]["cents"],
pledges[i]["transactions"]["outgoingAmount"]["currency"],
pledges[i]["transactions"]["outgoingVs"],
pledges[i]["transactions"]["outgoingBankAccount"]
]);
}
}
// Log the campaignData array
Logger.log(pledgesData);
// select the campaign output sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Pledges');
// calculate the number of rows and columns needed
var numRows = pledgesData.length;
var numCols = pledgesData[0].length;
// output the numbers to the sheet
sheet.getRange(2,1,numRows,numCols).setValues(pledgesData);
}
catch (error) {
// deal with any errors
Logger.log(error);
};
}
我不是编码员,所以我会感谢任何想法: - )