如何使用JSON和IEX API获得股票市场上涨幅最大的股票的列表

时间:2018-10-28 02:34:53

标签: json stock

我正在尝试编写一个Google脚本,该脚本使用IMPORTJSON函数来请求列出当天获得增值的前十只股票。

我将代码分成两个文件。一个文件具有IMPORTJSON函数,另一个文件具有请求我需要的数据的功能。

请求数据后,后一个功能通过电子邮件将其发送给我,但是我收到的电子邮件中显示:

  

“获取数据时出错”。

这是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";  
    }
 }

该功能应该用于请求数据并通过电子邮件发送:

function requestAndSendData() {

    // Get the email address of the active user - that's you.
    var email = Session.getActiveUser().getEmail();

    // Get the name of the document to use as an email subject line.
    var subject = 'runner ';

    // Append a new string to the "url" variable to use as an email body.
    var quote  =IMPORTJSON("https://api.iextrading.com/1.0//stock/market/list/gainers");
    // Send yourself an email with a link to the document.
    GmailApp.sendEmail(email, subject, quote);
}

我使用IEX API Documentation

中的IEX API编写了请求函数

我不确定为什么会出现此错误。我将不胜感激任何帮助。谢谢。

0 个答案:

没有答案