为什么这个 UrlFetch 函数不能正常工作?

Why is this UrlFetch function not working properly?

这是我试图通过我的 Google 电子表格图表上的 JavaScript 工具 运行 的功能,该功能目前在 4 个不同的网站上运行,在这个示例:

$id 是从我的电子表格单元格 (belacoin) 导入的值

function CoinmarketcapPrice($id) {
  var response = UrlFetchApp.fetch("https://api.coinmarketcap.com/v1/ticker/" + $id);
  var html = JSON.parse(response.getContentText());
  try 
  {
    return parseFloat(html["price_btc"]);
  }
  catch(err)
  {
    return null;
  }
}

这就是 UrlFetch returns:

[
    {
        "id": "belacoin", 
        "name": "BelaCoin", 
        "symbol": "BELA", 
        "rank": "176", 
        "price_usd": "0.212823", 
        "price_btc": "0.00008400", 
        "24h_volume_usd": "534995.0", 
        "market_cap_usd": "7694903.0", 
        "available_supply": "36156350.0", 
        "total_supply": "36156350.0", 
        "percent_change_1h": "0.63", 
        "percent_change_24h": "1.88", 
        "percent_change_7d": "-17.03", 
        "last_updated": "1499549044"
    }
]

看起来端点正在返回一个对象数组,所以如果您想访问数组中的第一个项目,请将您的 try 块更改为:

try {
  return parseFloat(html[0]["price_btc"]);
}

尝试html.price_btc或html[0]。price_btc

return html[0].price_btc;

成功了,谢谢大家!