无法加入JSON对象的适当位
我正在尝试调用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论