我正在使用 google appscript 嘗試使用 CoinMarketCap api 報價端點檢索單個貨幣的價格詳細資訊,而不是一次獲取 5000 對的串列,以免我在不到一周的時間內耗盡我的 api 積分。但由于某種原因,回傳的報價是
報價:{美元:[物件]}}
給出回傳的 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] } },
如何檢索我的貨幣的實際報價數字?
uj5u.com熱心網友回復:
根據API Documentation,quote結構是:
"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物件轉換為陣列,您可以迭代/回圈資料,如上所示。
學到更多:
- 物件條目()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/484854.html
