Javascript 需要在JSON格式的API输出中搜索值并打印整个数组内容

Javascript 需要在JSON格式的API输出中搜索值并打印整个数组内容,javascript,node.js,json,google-sheets,Javascript,Node.js,Json,Google Sheets,我有googlesheets函数可以解析json并导入到工作表中,您可以在ImportJson文件中找到该函数的代码 function IMPORTJSON(url,xpath){ try{ // /rates/EUR var res = UrlFetchApp.fetch(url); var content = res.getContentText(); var json = JSON.parse(content); var patharray

我有googlesheets函数可以解析json并导入到工作表中,您可以在ImportJson文件中找到该函数的代码

function IMPORTJSON(url,xpath){

  try{
    // /rates/EUR
    var res = UrlFetchApp.fetch(url);
    var content = res.getContentText();
    var json = JSON.parse(content);

    var patharray = xpath.split("/");
    //Logger.log(patharray);

    for(var i=0;i<patharray.length;i++){
      json = json[patharray[i]];
    }

    //Logger.log(typeof(json));

    if(typeof(json) === "undefined"){
      return "Node Not Available";
    } else if(typeof(json) === "object"){
      var tempArr = [];

      for(var obj in json){
        tempArr.push([obj,json[obj]]);
      }
      return tempArr;
    } else if(typeof(json) !== "object") {
      return json;
    }
  }
  catch(err){
      return "Error getting data";  
  }

}
您还可以在此链接“”上检查此API的实时版本

要获得“2019-03-30”上的DCF值,我可以简单地使用如下函数:

=IMPORTJSON("https://financialmodelingprep.com/api/v3/company/historical-discounted-cash-flow/AAPL?period=quarter","historicalDCF/0/DCF")
如果我需要搜索日期并获得股票价格的值,该怎么办?例如,我需要获得该日期“2017-09-30”的股票价格和DCF值。在不知道阵列位置的情况下我怎么做

为此,我需要通过创建新函数或修改现有函数来获得此功能的帮助

帮助是高度赞赏和感谢所有的大师提前在那里

json.historicalDCF.filter((el)=>el.date==“2017-07-01”)将返回与该日期匹配的对象

你可以得到如下回报:


function findByDate(dateParam) {
  return json.historicalDCF.filter((el) => el.date === dateParam)

}

var theInfoIWantVariable = findByDate("2017-1-1-make-sure-this-is-a-string")


假设您的导入函数返回一个JSON对象(并且假设格式与您在文章中显示的格式相同),那么您只需使用常规javascript
filter()
函数即可获得所需的特定日期:

const data =IMPORTJSON("https://financialmodelingprep.com/api/v3/company/historical-discounted-cash-flow/AAPL?period=quarter","historicalDCF");
// now, data is the JSON array containing the historical data. All the data. We can filter it for whatever we want.
const myOneDayRecord = data.find( record => record.date === '2016-03-30' );
// so the variable myOneDayRecord is the specific record by date we wanted. Now we can use it as a normal javascript object.
console.log(`So on ${myOneDayRecord.date}, the stock price was ${myOneDayRecord["Stock Price"] } and the DCF was ${myOneDayRecord.DCF}`);
// note that, in the above line,I had to use bracket notation to get to the 'Stock Price' property. That's because of the space in the property name.

如果我理解正确,您希望创建一个自定义函数,该函数将:

  • 接受
    url
    xpath
    date
    作为参数
  • 调用
    IMPORTJSON
    并将前面的
    url
    xpath
    作为参数传递
  • 在由
    IMPORTJSON
    返回的JSON中,在
    historicalDCF
    中查找其
    date
    与作为参数传递的项匹配的项
  • 返回其相应的
    股价
如果所有的abose都是正确的,那么你可以按照以下思路做一些事情:

函数GETSTOCKPRICE(url、xpath、日期){ var jsonData=IMPORTJSON(url,xpath); var historical=jsonData[“historicalDCF”]; var股价; 对于(变量i=0;i 然后,您可以通过传递正确的参数在工作表中使用此函数。例如,如果您希望获得2017-09-30对应的股票价格:

=GETSTOCKPRICE("https://financialmodelingprep.com/api/v3/company/historical-discounted-cash-flow/AAPL?period=quarter", "historicalDCF/0/DCF", "2017-09-30")
笔记:
  • filter
    find
    这样的方法,像
    const
    这样的箭头函数和声明在这里不起作用,因为Apps脚本目前不支持ES6
  • 您还可以修改
    GETSTOCKPRICE
    ,使其只接受
    date
    作为参数,并且对
    IMPORTJSON
    的调用将其参数硬编码(如果您总是要使用
    IMPORTJSON
    来处理这个确切的
    url
    xpath
    ,这将是有意义的)。但这取决于你

我希望这能有所帮助。

您正在应用程序脚本中运行此功能,对吗?为什么将其标记为
node.js
=GETSTOCKPRICE("https://financialmodelingprep.com/api/v3/company/historical-discounted-cash-flow/AAPL?period=quarter", "historicalDCF/0/DCF", "2017-09-30")