为什么这个UrlFetch功能无法正常工作? 2.0

时间:2017-07-09 21:13:31

标签: javascript google-sheets fetch google-spreadsheet-api urlfetch

这是我试图通过我的Google电子表格图表上的JavaScript工具运行的功能,我有一个非常类似的功能,目前正在另一个网站上工作,我之前已经解决了这个主题的问题: Why is this UrlFetch function not working properly?

但是这看起来非常相似,我无法让它发挥作用......

$ id是我的电子表格单元格(BTC_BELA)

中的导入值
function NovaexchangePrice($id) {
  var response = UrlFetchApp.fetch("https://novaexchange.com/remote/v2/markets/");
  var html = JSON.parse(response.getContentText());
  try 
  {
    return parseFloat(html['markets']["last_price"]);
  }
  catch(err)
  {
    return null;
  }
}

我目前尝试了以下内容:

return parseFloat(html['markets'].last_price);
return parseFloat(html[2].last_price);
return parseFloat(html.markets[1]);

这是UrlFetch(https://novaexchange.com/remote/v2/markets/)返回的内容:

{"status": "success", "message": "Markets listed at Novaexchange", "markets":
 [{"bid": "0.00000602", "last_price": "0.00000600", "volume24h": "0.333", "marketid": 7539, "disabled": 1, "currency": "4CHN", "marketname": "BTC_4CHN", "ask": "0.00001000", "low24h": "0.00000600", "change24h": "-64.7", "high24h": "0.00001700", "basecurrency": "BTC"},
 {"bid": "0.00000000", "last_price": "0.00000001", "volume24h": "0.000", "marketid": 7347, "disabled": 1, "currency": "SPRT", "marketname": "BTC_SPRT", "ask": "0.00000001", "low24h": "0.00000001", "change24h": "0.0", "high24h": "0.00000001", "basecurrency": "BTC"},
 {"bid": "0.00000000", "last_price": "0.00000001", "volume24h": "0.000", "marketid": 7359, "disabled": 1, "currency": "XPZ", "marketname": "BTC_XPZ", "ask": "0.00000004", "low24h": "0.00000001", "change24h": "0.0", "high24h": "0.00000001", "basecurrency": "BTC"},
... and so on.

1 个答案:

答案 0 :(得分:0)

您尝试html.markets[0].last_price

编辑:

如果您只是在寻找一个特定的市场,那么您可以在这样的市场中进行forEach循环:

html.markets.forEach(function(entry) {
    if (entry.marketname == "BTC_BELA") {
        console.log(entry.last_price);
    }
});

尽管将每个条目重新分配给键控对象以便于访问可能会更好:

var obj = {};
html.markets.forEach(function(entry) {
    var marketname = entry.marketname;
    obj[marketname] = entry;
});

console.log(obj["BTC_BELA"].last_price);