Reach Fetching Datas from external API


#1

Hi guys, I got a small problem.
I would like to get with an external API some json datas but there is a trap.

{
"coord":{"lon":-73.57,"lat":45.5},
"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],
"base":"stations","main":{"temp":22.91,"pressure":1010,"humidity":60,"temp_min":22,"temp_max":24},
"visibility":48279,
"wind":{"speed":7.7,"deg":270,"gust":11.3},
"clouds":{"all":20},
"dt":1526320800,
sys":"type":1,"id":3829,"message":0.0902,"country":"CA",
"sunrise":1526289839,"sunset":1526343480},
id":6077243,
"name":"Montreal","cod":200}

That’s the json data of a small weather API but i don’t even know how to get precisely a object like in Coord -> lon.

In my code I put :

	loadPosition(){
 
		this.geolocation.getCurrentPosition().then((resp) => {
		 // resp.coords.latitude
		 //let userPosition: LatLng = news LatLng(resp.coords.latitude,resp.coords.longitude);
		 //console.log(resp.coords.latitude + ' ' + resp.coords.longitude);\
		  this.lng = resp.coords.longitude;
			this.lat = resp.coords.latitude;
			this.test = this.Pro.getWeatherPerLatLng(this.lat,this.lng).subscribe((data: Weather) => this.weather.coord.lon = {lon: data["lon"],
		});
			console.log(this.test, 'Sortie de la fonction');
			console.log('Weather Object', this.weather);
		});

But i don’t know how to get the “LON” in '‘Coord’…

I created an interface like that

export interface Weather{
  coord: {
    lon: number,
    lat: number
  },
  weather: [
    {
      id: number,
      main: String,
      description: String,
      icon: String
    }
  ],
  base: String,
  main: {
    temp: number,
    pressure: number,
    humidity: number,
    temp_min: number,
    temp_max: number
  },
  visibility: number,
  wind: {
    speed: number,
    deg: number
  },
  clouds: {
    all: number
  },
  dt: number,
  sys: {
    type: number,
    id: number,
    message: number,
    country: String,
    sunrise: number,
    sunset: number
  },
  id: number,
  name: String,
  cod: number
}

Thanks for your time …


#2

See:


#3

Hi, thanks for your reply but if i’m doing a parse, shoud I delete the line with the subscribe ?


#4

JSON data is normally accessed in Javascript through dot notation.

See: https://www.digitalocean.com/community/tutorials/how-to-work-with-json-in-javascript


#5

Yes i do but always saying “Unable to assign type {lon: any} to type number”
I think i’m doing it really wrong…