无法加入JSON对象的适当位

发布于 2025-02-13 08:21:08 字数 995 浏览 1 评论 0原文

我正在尝试调用API并从中获取JSON数据。当我获取数据时,我无法显示对象的属性。

 useEffect(() => {
  fetch(url)
  .then(response => response.json())
  .then(actualData => {
    console.log(actualData)
    setData(actualData)
  }).catch((err) => {
    setError(err.message)
  })
}, [])
return (
<div>
  <h3>{JSON.stringify(data.name)}</h3>
  <p>{error}</p>
  <Link to='/'>Home</Link>
</div>

console.log输出:

{"coord":{"lon":23.5833,"lat":46.0667},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":21.61,"feels_like":21.81,"temp_min":21.61,"temp_max":21.61,"pressure":1014,"humidity":76,"sea_level":1014,"grnd_level":989},"visibility":10000,"wind":{"speed":1.99,"deg":244,"gust":3.92},"clouds":{"all":100},"dt":1657127565,"sys":{"country":"RO","sunrise":1657075288,"sunset":1657131535},"timezone":10800,"id":686578,"name":"Alba Iulia","cod":200}

I'm trying to call an API and get the JSON data from that. When I get the data I can't display the properties of the object.

 useEffect(() => {
  fetch(url)
  .then(response => response.json())
  .then(actualData => {
    console.log(actualData)
    setData(actualData)
  }).catch((err) => {
    setError(err.message)
  })
}, [])
return (
<div>
  <h3>{JSON.stringify(data.name)}</h3>
  <p>{error}</p>
  <Link to='/'>Home</Link>
</div>

The console.log output:

{"coord":{"lon":23.5833,"lat":46.0667},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":21.61,"feels_like":21.81,"temp_min":21.61,"temp_max":21.61,"pressure":1014,"humidity":76,"sea_level":1014,"grnd_level":989},"visibility":10000,"wind":{"speed":1.99,"deg":244,"gust":3.92},"clouds":{"all":100},"dt":1657127565,"sys":{"country":"RO","sunrise":1657075288,"sunset":1657131535},"timezone":10800,"id":686578,"name":"Alba Iulia","cod":200}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文