通过 GS 获取数据 - 响应代码 200 但数据总是 "undefined"

Fetch data via GS - response code 200 but data always "undefined"

我尝试获取 https://api.llama.fi/charts/Ethereum

给出的数据

看起来像

[{"date":"1546560000","totalLiquidityUSD":273845651.27077854},{"date":"1546646400","totalLiquidityUSD":288674544.41292274},{"date":"1546732800","totalLiquidityUSD":297321259.6930144},{"date":"1546819200","totalLiquidityUSD":286168221.103729},{"date":"1546905600","totalLiquidityUSD":285073686.76345384}, ...

当我在 chrome 中打开它时。

在 python 和 urllib.request.urlopen() 中工作正常。

这里是 Google 我试过的表格

function myFunction() {
  var data = UrlFetchApp.fetch("https://api.llama.fi/charts/Ethereum").getResponseCode()
  var data2 = UrlFetchApp.fetch("https://api.llama.fi/charts/Ethereum").getContentText()
  console.log(UrlFetchApp.fetch("https://api.llama.fi/charts/Ethereum").getContentText())
}

此处数据 = 200,但数据 2 = 未定义。我觉得是菜鸟问题,我对JS和GS都不熟悉

感谢您的帮助!

最佳,

多米尼克

您可以通过将 ContentText 解析为 JSON 来访问作为 JSON 对象的数据,然后您可以使用 for 迭代数据或以您需要的任何其他方式使用它。

function myFunction() {
  var response = UrlFetchApp.fetch("https://api.llama.fi/charts/Ethereum").getContentText()
  
  var data = JSON.parse(response);
  for(const d of data) {
    console.log("DATE: " + d.date)
    console.log("LIQUIDITY: " + d.totalLiquidityUSD)
  }
}