从Coinmarketcap API获取Json特定的变量数据

时间:2019-06-04 17:06:05

标签: json google-apps-script google-sheets

我正在设置一个Google表格脚本,以从coinmarketcap API获取硬币价格。使用Json,我能够获得硬币价格,但是当硬币URL更改时,脚本会出错。

function getCryptoPrice(coin) {

  var url="https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=" + coin;

  var requestOptions = {
  method: 'GET',
  uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
  qs: {
    start: 1,
    limit: 5000,
    convert: 'USD'
  },
  headers: {
    'X-CMC_PRO_API_KEY': 'MY-API-KEY'
  },
  json: true,
  gzip: true
};

  var httpRequest= UrlFetchApp.fetch(url, requestOptions);
  var getContext= httpRequest.getContentText();

  var parseData=JSON.parse(getContext);

  return parseData.data.GVT.quote.USD.price;
}

当“硬币”更改时,URL也随之更改,但“ return parseData.data.GVT"...”仍获得相同的硬币符号“ GVT”。如何修改

return parseData.data.GVT.quote.USD.price

获得GVT符号更改时的硬币价格?

Json解析文本:

{data={GVT={symbol=GVT, circulating_supply=4426883.31189056, last_updated=2019-06-04T14:49:03.000Z, total_supply=4436643.92853333, cmc_rank=260, platform={symbol=ETH, name=Ethereum, token_address=0x103c3a209da59d3e7c4a89307e66521e081cfdf0, id=1027, slug=ethereum}, tags=[], date_added=2017-11-15T00:00:00.000Z, quote={USD={percent_change_1h=-0.00743386, last_updated=2019-06-04T14:49:03.000Z, percent_change_24h=-4.46141, market_cap=1.494379345295687E7, price=3.37569174521, volume_24h=1445068.31606641, percent_change_7d=-3.74407}}, num_market_pairs=17, name=Genesis Vision, max_supply=null, id=2181, slug=genesis-vision}}, status={error_message=null, elapsed=45, credit_count=1, error_code=0, timestamp=2019-06-04T14:49:56.313Z}}

2 个答案:

答案 0 :(得分:0)

使用括号表示法访问数据:

parseData.data[coin].quote.USD.price;

答案 1 :(得分:0)

如果你只是这样做是不是更容易

=IMPORTDATA("https://cryptoprices.cc/ETH")