如何从 openweathermap 的 Json 响应中读取数据
我从 Openweathermaps API 得到这样的响应,我真的不知道如何获取更深层次的数据,我正在反应原生。这是我用来从 API 获取数据的函数。
const getWeatherData = async (savedPlacesData) =>{
if(dataToRender!=null){
dataToSave = dataToRender
}
console.log(savedPlacesData)
const options = {
method: 'GET',
url: 'https://community-open-weather-map.p.rapidapi.com/weather',
params: {q: savedPlacesData.name, callback: 'test', lang: 'null', units: 'metric'},
headers: {
'x-rapidapi-host': 'community-open-weather-map.p.rapidapi.com',
'x-rapidapi-key': 'e01436d70bmsh6fcef67bef75173p1660f2jsncb27dc744b4b'
}
};
axios.request(options).then(function (response) {
console.log(response.data)
setDataToRender(dataToSave);
}).catch(function (error) {
console.error(error);
});
}
这是我得到的响应数据。
test({
"coord":{
"lon":21.0744,
"lat":50.3096
},
"weather":[
{
"id":803,
"main":"Clouds",
"description":"broken clouds",
"icon":"04d"
}
],
"base":"stations",
"main":{
"temp":1.2,
"feels_like":-3.9,
"temp_min":1.2,
"temp_max":1.2,
"pressure":1034,
"humidity":58,
"sea_level":1034,
"grnd_level":1014
},
"visibility":10000,
"wind":{
"speed":5.94,
"deg":70,
"gust":7.65
},
"clouds":{
"all":57
},
"dt":1646037631,
"sys":{
"country":"PL",
"sunrise":1646025730,
"sunset":1646064885
},
"timezone":3600,
"id":757388,
"name":"Szczucin",
"cod":200
})
例如,如何获取天气描述?
I get a response like this from Openweathermaps API, and i honsetly don't know how to get to data that is deeper, I'm working in react-native. This is my function that I use to get the data from the API.
const getWeatherData = async (savedPlacesData) =>{
if(dataToRender!=null){
dataToSave = dataToRender
}
console.log(savedPlacesData)
const options = {
method: 'GET',
url: 'https://community-open-weather-map.p.rapidapi.com/weather',
params: {q: savedPlacesData.name, callback: 'test', lang: 'null', units: 'metric'},
headers: {
'x-rapidapi-host': 'community-open-weather-map.p.rapidapi.com',
'x-rapidapi-key': 'e01436d70bmsh6fcef67bef75173p1660f2jsncb27dc744b4b'
}
};
axios.request(options).then(function (response) {
console.log(response.data)
setDataToRender(dataToSave);
}).catch(function (error) {
console.error(error);
});
}
And this is the response data that I get.
test({
"coord":{
"lon":21.0744,
"lat":50.3096
},
"weather":[
{
"id":803,
"main":"Clouds",
"description":"broken clouds",
"icon":"04d"
}
],
"base":"stations",
"main":{
"temp":1.2,
"feels_like":-3.9,
"temp_min":1.2,
"temp_max":1.2,
"pressure":1034,
"humidity":58,
"sea_level":1034,
"grnd_level":1014
},
"visibility":10000,
"wind":{
"speed":5.94,
"deg":70,
"gust":7.65
},
"clouds":{
"all":57
},
"dt":1646037631,
"sys":{
"country":"PL",
"sunrise":1646025730,
"sunset":1646064885
},
"timezone":3600,
"id":757388,
"name":"Szczucin",
"cod":200
})
how to get to weather description for example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能从参数中删除
callback: 'test'
。这样您就可以访问天气描述probably remove
callback: 'test'
from the param. That way you would be able to access the weather description