如何从 Google 脚本中的 JSON 字符串获取特定值?
How do I get specific value from JSON string in Google Script?
我正在尝试从 this json file 中提取特定值:
我正在寻找的示例值是 exDividendDate,fmt:2020-09-24。
我为提取值而编写的代码不会提取此值或任何其他值,我不确定为什么。任何帮助将不胜感激。
我在 Google Apps 脚本中遇到的错误是:
TypeError: Cannot read property 'earningsDate' of undefined (line 44,
file "Stock Database"
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数据时,好像exDividendDate
是callAPI(symbol).quoteSummary.result[0].calendarEvents
。那么下面的修改呢?
发件人:
var results = api[0];
收件人:
var results = api.quoteSummary.result[0].calendarEvents;
我正在尝试从 this json file 中提取特定值:
我正在寻找的示例值是 exDividendDate,fmt:2020-09-24。
我为提取值而编写的代码不会提取此值或任何其他值,我不确定为什么。任何帮助将不胜感激。
我在 Google Apps 脚本中遇到的错误是:
TypeError: Cannot read property 'earningsDate' of undefined (line 44, file "Stock Database"
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数据时,好像exDividendDate
是callAPI(symbol).quoteSummary.result[0].calendarEvents
。那么下面的修改呢?
发件人:
var results = api[0];
收件人:
var results = api.quoteSummary.result[0].calendarEvents;