【问题标题】:How do I access this object in JSON如何在 JSON 中访问此对象
【发布时间】:2020-07-16 07:42:29
【问题描述】:

我正在尝试在 Javascript 中访问 weather 对象。我试过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
});

【问题讨论】:

  • JSON.stringify 并没有像你想象的那样做:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… output 变量只是一个字符串;除了打印它,你什么也做不了。
  • JSON.stringify 把它变成一个字符串
  • JSON.stringify 会将 JSON 转换为字符串,只需删除 JSON.stringify 函数并执行 console.log(output)

标签: javascript node.js json weather


【解决方案1】:

问题在于 output 是一个字符串,而不是一个对象。你的意思是 JSON.parse() 吗?从字符串中输出一个对象。

【讨论】:

  • 我尝试了 JSON.parse(),但出现错误。 SyntaxError: Unexpected token o in JSON at position 1
  • 结果的值是多少?是字符串还是对象?
【解决方案2】:

根据weather-jsdocumentationresult 已经是一个对象,所以你要做的就是访问它:

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

【讨论】:

  • 非常感谢!没注意到!
【解决方案3】:

我认为您需要在 JSON.stringify() 之后解析字符串,因为 JSON stringify 将对象更改为字符串。

参考:JSON StringifyJSON Parse

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 2015-04-20
    • 2022-07-06
    • 1970-01-01
    • 2017-08-16
    • 2012-06-06
    • 2021-11-29
    相关资源
    最近更新 更多