如何从Google脚本中的JSON字符串中获取特定值? 发件人:收件人:

问题描述

我正在尝试从this json file提取特定值:

我正在寻找的示例值是exDividendDate,fmt:2020-09-24。

我编写的用于提取值代码不会提取该值或任何其他值,我不确定为什么。任何帮助将不胜感激。

我在Google Apps脚本中遇到的错误是:

TypeError:无法读取未定义的属性“ earningsDate”(第44行, 文件“库存数据库

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];