简体   繁体   中英

How do I access this object in JSON

I'm trying to access the weather object in Javascript. I've tried output.weather but it returns undefined . What am I doing wrong?

JSON:

[
   {
      "location":{
         "name":"XX",
         "zipcode":"XX",
         "lat":"42.284",
         "long":"-82.029",
         "timezone":"-4",
         "alert":"",
         "degreetype":"C",
         "imagerelativeurl":"http://blob.weather.microsoft.com/static/weather4/en-us/"
      },
      "current":{
         "temperature":"2",
         "skycode":"31",
         "skytext":"Clear",
         "date":"2020-04-03",
         "observationtime":"23:45:00",
         "observationpoint":"N0P 1E0, ON",
         "feelslike":"1",
         "humidity":"84",
         "winddisplay":"6 km/h North",
         "day":"Friday",
         "shortday":"Fri",
         "windspeed":"6 km/h",
         "imageUrl":"http://blob.weather.microsoft.com/static/weather4/en-us/law/31.gif"
      },
      "forecast":[
         {
            "low":"2",
            "high":"9",
            "skycodeday":"31",
            "skytextday":"Clear",
            "date":"2020-04-03",
            "day":"Friday",
            "shortday":"Fri",
            "precip":"0"
         },
         {
            "low":"2",
            "high":"11",
            "skycodeday":"32",
            "skytextday":"Sunny",
            "date":"2020-04-04",
            "day":"Saturday",
            "shortday":"Sat",
            "precip":"60"
         },
         {
            "low":"0",
            "high":"8",
            "skycodeday":"30",
            "skytextday":"Partly Sunny",
            "date":"2020-04-05",
            "day":"Sunday",
            "shortday":"Sun",
            "precip":"60"
         },
         {
            "low":"5",
            "high":"11",
            "skycodeday":"30",
            "skytextday":"Partly Sunny",
            "date":"2020-04-06",
            "day":"Monday",
            "shortday":"Mon",
            "precip":"30"
         },
         {
            "low":"9",
            "high":"13",
            "skycodeday":"26",
            "skytextday":"Cloudy",
            "date":"2020-04-07",
            "day":"Tuesday",
            "shortday":"Tue",
            "precip":"80"
         }
      ]
   }
]

JS CODE:

const rl = require('readline-sync');
const weather = require('weather-js');

var zipCode = rl.question('Please enter your ZIP code: ').toLowerCase();
var output;

weather.find({search: zipCode, degreeType: 'F'}, function(err, result) {
    if (err)
    {
        console.log(err);
    }

    output = JSON.stringify(result);
    console.log(output.weather); // returns undefined
});

The issue is that output is a string, not an object. Did you mean JSON.parse()? That outputs an object from a string.

According to weather-js documentation , result is already an object, so all you have to do is access it:

weather.find({search: zipCode, degreeType: 'F'}, function(err, result) {
    console.log("Low: " + result[0].forecast[0].low);
});

I think you need to parse the string after JSON.stringify() because JSON stringify change the object to a string.

Ref: JSON Stringify , JSON Parse

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM