-2

So, I am a newbie to js and don't have much knowledge of JSON... I was making a speech-assistant kind of thing with node and when I came upon weather I googled it...I just ctrl+c ctrl+v somethings (I am just 14) The output I am getting is

[
  {
    location:
 {
      name: 'Chennai, India',
      zipcode: undefined,
      lat: '13.012',
      long: '80.221',
      timezone: '5.5',
      alert: '',
      degreetype: 'F',
      imagerelativeurl: 'http://blob.weather.microsoft.com/static/weather4/en-us/'
    },
    current: {
      temperature: '86',
      skycode: '31',
      skytext: 'Mostly Clear',
      date: '2020-07-24',
      observationtime: '22:00:00',
      observationpoint: 'Chennai, India',
      feelslike: '99',
      humidity: '79',
      winddisplay: '8 mph South',
      day: 'Friday',
      shortday: 'Fri',
      windspeed: '8 mph',
      imageUrl: 'http://blob.weather.microsoft.com/static/weather4/en-us/law/31.gif'
    },
    forecast: [ [Object], [Object], [Object], [Object], [Object] ]
  },
  {
    location: {
      name: 'Chennai, India',
      zipcode: undefined,
      lat: '13.072',
      long: '80.202',
      timezone: '5.5',
      alert: '',
      degreetype: 'F',
      imagerelativeurl: 'http://blob.weather.microsoft.com/static/weather4/en-us/'
    },
    current: {
      temperature: '86',
      skycode: '31',
      skytext: 'Mostly Clear',
      date: '2020-07-24',
      observationtime: '22:00:00',
      observationpoint: 'Chennai, India',
      feelslike: '99',
      humidity: '79',
      winddisplay: '8 mph South',
      day: 'Friday',
      shortday: 'Fri',
      windspeed: '8 mph',
      imageUrl: 'http://blob.weather.microsoft.com/static/weather4/en-us/law/31.gif'
    },
    forecast: [ [Object], [Object], [Object], [Object], [Object] ]
  }
]

(if you don't like the look of this then--> https://docs.google.com/document/d/1jW0eqrgAfYQwqpuA3SDULwfwpWbyKi1CDwNAVnN2YT8/edit?usp=sharing )

Can anyone please help me get the values of things like temperature, sky text, humidity and same for the forecast

Thanks a lot...

  • I find that it is hard to find a problem in code, if you don't know what the code is. So please include some. – luek baja Jul 24 '20 at 17:41

3 Answers3

0

Suppose you store this whole array in the variable named myData then to get the value of temperature from each object, you can use the following code.

let temperature=[]    
myData.forEach((data)={
   temperature.push(data.current.temperature)
       })

Similarly you can get other values also whatever you want.

Minal Shah
  • 1,402
  • 1
  • 5
  • 13
0

This is actually a JavaScript object.

So you can easily access the values like:

object[0].location.current.temperature;
lhrinaldi
  • 53
  • 5
0

JSON.parse(YOUR_JSON_OBJECT) this will parse the JSON. JSON.stringify(YOUR_JSON_OBJECT) this is convert the JSON in to a string.

JSON Objects are just pair of key and values where you can access the value using key for example

const a={key1:1,key2:2};

here to get the value of a you can do

a.key1;

JSON Objects may be nested as they are in your condition.For example

const a={ key1:1, key2:{ key2a:1, key2b:2 } }

so to access the value from key2b key you can do like this

b.key2.key2a