【问题标题】:Reading JSON Files读取 JSON 文件
【发布时间】:2020-11-12 12:56:56
【问题描述】:

在尝试让coin ranking api 工作时遇到问题,但已经卡住了。 我一直在使用 fetch 将 JSON 记录到控制台中,但无法弄清楚如何进入某个元素。

fetch('https://api.coinranking.com/v1/public/coin/1')   
.then(res => res.json())
.then(data => console.log(data))

我只想要来自 'data['coin']' 的数据,而不是记录所有数据

【问题讨论】:

  • .then(result => result.data.coin) 现在你有了一个可以解决你想要的承诺

标签: javascript json parsing


【解决方案1】:

看到您正在使用的 API 的响应,“coin”在“data”中,所以:

fetch('https://api.coinranking.com/v1/public/coin/1')   
.then(res => res.json())
.then(data => console.log(data.data.coin))

【讨论】:

  • 非常感谢,没想到答案这么明显!
  • 你也可以考虑用Axios代替fetch(),这样就省去了额外的.then(res => res.json())步骤
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 2014-03-31
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 2022-11-27
  • 2011-10-28
相关资源
最近更新 更多