无法使用 google appscript 中的 CoinMarketCap api 端点'/v1/cryptocurrency/quotes/latest' return 报价中的详细信息

Not able to return price detail in quote using CoinMarketCap api endpoint '/v1/cryptocurrency/quotes/latest' in google appscript

我正在使用 google appscript 尝试使用 CoinMarketCap api 报价端点检索单个货币的价格详细信息,而不是一次获取 5000 对的列表,以免我干涸在不到一周的时间内用完了我这个月的 api 学分。但由于某种原因,返回的报价是

quote: { USD: [Object] } }

摘录返回的 JSON:

{ BTC: 
   { id: 1,
     name: 'Bitcoin',
     symbol: 'BTC',
     slug: 'bitcoin',
     num_market_pairs: 9466,
     date_added: '2013-04-28T00:00:00.000Z',
     tags: 
      [ 'mineable',
        'pow',
        'sha-256',
....
....
     self_reported_circulating_supply: null,
     self_reported_market_cap: null,
     last_updated: '2022-05-27T04:37:00.000Z',
     quote: { USD: [Object] } },

如何检索我的货币的实际报价数字?

根据 API Documentationquote 结构是:

"quote": {
  "USD": {
    "price": 6602.60701122,
    "volume_24h": 4314444687.5194,
    "volume_change_24h": -0.152774,
    "percent_change_1h": 0.988615,
    "percent_change_24h": 4.37185,
    "percent_change_7d": -12.1352,
    "percent_change_30d": -12.1352,
    "market_cap": 852164659250.2758,
    "market_cap_dominance": 51,
    "fully_diluted_market_cap": 952835089431.14,
    "last_updated": "2018-08-09T21:56:28.000Z"
  }
}

有效记录对象的一个​​有用方法是:

Logger.log(JSON.stringify(theObject))

另外:

const key = `...`
const url = `...`
const options = {
  method: "GET",
  headers: { 
    "X-CMC_PRO_API_KEY": key 
  },
  json: true,
  gzip: true,
}

const response = UrlFetchApp.fetch(url, options).getContentText()
const coinData = Object.entries(JSON.parse(response).data)

for (let [coin, data] of coinData) {
  Logger.log(`${coin}: ${data.quote.USD.price}`)
} 

data对象转成数组,就可以iterate/loop如上图的数据

了解更多: