簡體   English   中英

如何在 JSON 中訪問此 object

[英]How do I access this object in JSON

我正在嘗試訪問 Javascript 中的weather object。 我試過output.weather但它返回undefined 我究竟做錯了什么?

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代碼:

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
});

問題是output是一個字符串,而不是 object。 您是說 JSON.parse() 嗎? 從字符串中輸出 object。

根據weather-js文檔result已經是 object,所以您只需訪問它:

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

我認為您需要在 JSON.stringify() 之后解析字符串,因為 JSON stringify 將 object 更改為字符串。

參考: JSON 字符串化JSON 解析

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM