问题描述
我正在尝试从this json file中提取特定值:
我正在寻找的示例值是exDividendDate,fmt:2020-09-24。
我编写的用于提取值的代码不会提取该值或任何其他值,我不确定为什么。任何帮助将不胜感激。
我在Google Apps脚本中遇到的错误是:
function callAPI(symbol) {
// Call the API
var url = 'https://query2.finance.yahoo.com/v10/finance/quoteSummary/'
var modules = "?modules=calendarEvents"
var response = UrlFetchApp.fetch(url + symbol + modules);
// Parse the JSON reply
var json = response.getContentText();
var data = JSON.parse(json);
console.log(data)
return JSON.parse(json)
}
function displayFinancials() {
// Load sheets
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Results");
var modelSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Financial Ratios Model");
// Get model input data
var company = "Apple"
var symbol = "AAPL"
// call the API
var api = callAPI(symbol);
var results = api[0];
// Output the API result
var output = [company,symbol,results.exDividendDate.fmt]
console.log(output);
dataSheet.appendRow(output)
}
解决方法
当我看到JSON数据时,看来switch (theKey) {
case "AltLeft":
case "CapsLock":
case "ControlRight":
case "Fn":
case "NumpadDecimal":
...
是exDividendDate
。那么下面的修改如何?
发件人:
callAPI(symbol).quoteSummary.result[0].calendarEvents
收件人:
var results = api[0];