如何从 openweathermap 的 Json 响应中读取数据

发布于 2025-01-10 17:18:49 字数 1622 浏览 4 评论 0原文

我从 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2025-01-17 17:18:49

可能从参数中删除 callback: 'test' 。这样您就可以访问天气描述

probably remove callback: 'test' from the param. That way you would be able to access the weather description

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文