How to write JSON get requests (Weather Project)?

How to write JSON get requests (Weather Project)?
0

#1

Hello,
I am currently working on the current weather project, but I have a couple of questions. First off, I am unsure how to structure the JSON get requests in order to bring in the data from the api. Secondly, I have seen other users say that the open weather api doesn’t work with codepen, so I was thinking of using this api instead https://developer.forecast.io/docs/v2#forecast_call . The only thing is that the documentation mentions the names of the data attributes, but I am not experienced enough yet to see how to properly access this data with json/jquery. I have been extensively googling for a while, but I still haven’t found any resources that talk about how to set up requests for a given api; they only show the format of the data, but never any request syntax. Can someone help me with the syntax required to make general requests? Any specific hints to either of the apis mentioned would also be greatly appreciated.
Thanks!


#2

First thing with the weather project - codepen does work with OpenWeatherMap; it does not work with navigator.geolocation. (See my answer here for more info).

The JavaScript api gives you simply an object, and you can treat it as such. Ex:
{
test1: value;
test2: value;
}

To access the api use the jQuery method (FCC challenge):
`

  $.getJSON(url + apiKey, function(json) {
      // code here
  });`

the function argument “json” in this case is the api object that is parsed and returned. Now you can modify this object like you would any other object ex.
`

  $.getJSON(url + '?lat=' + lat + '&lon=' + lon + "&appid=" + apiKey, function(json) {   
    var temp = json.main.temp; //unit is kelvin
  });`

#3

Ok. I have managed to see the structure of both the open weather api and the ip geolocator api that you mentioned. Now that I have done that, I have another question. How do I access the lat and long elements of the ip geolocation api and make them into variables so that I can insert them into the getJSON function for the open weather api call? What would the syntax of that look like?
Thanks!


#4

In order for the open weather API that I’m using to access the data I grabbed from the geolocation API, do I need to nest the open weather api getJSON function inside the getJSON for the geolocation?


#5

The variable scope seems to be limited to inside each getJSON function