从Coinmarketcap API获取特定于Json的变量数据 [英] Get Json specific variable data from Coinmarketcap API

查看:987
本文介绍了从Coinmarketcap API获取特定于Json的变量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm setting up a google sheet script to get coin prices from coinmarketcap API. With Json I'm able to get the coin price but when the coin URL change the script gives an error.

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时,URL也更改,但"return parseData.data.GVT"..."仍然获得相同的硬币符号"GVT".我该如何编辑

When "coin" changes the URL change too but "return parseData.data.GVT"..." still get the same coin symbol "GVT". How can I edit

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

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

to get the coin price when the symbol GVT change?

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}}

推荐答案

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

Use bracket notation to access the data:

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

这篇关于从Coinmarketcap API获取特定于Json的变量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆