weather-js从打印的内容中提取特定数据

时间:2018-07-29 16:16:13

标签: javascript json node.js

我正在使用weather-js来收集由用户的IP地址确定的用户位置的天气数据。我想知道是否有一种方法可以简化控制台中打印的数据。除了它的主要功能“ weather.find”之外,此api没有任何文档或解释:

 weather2.find({search: location, degreeType: 'F'}, function(result, current) {
        if(err) console.log(err);
        console.log(JSON.stringify(result, null, 2));
      });

location是从数据库中提取的预定变量,这是打印出来的内容,

[
  {
    "location": {
      "name": "Larchmont, NY",
      "lat": "40.928",
      "long": "-73.751",
      "timezone": "-4",
      "alert": "",
      "degreetype": "F",
      "imagerelativeurl": "http://blob.weather.microsoft.com/static/weather4/en-us/"
    },
    "current": {
      "temperature": "78",
      "skycode": "34",
      "skytext": "Mostly Sunny",
      "date": "2018-07-29",
      "observationtime": "11:45:00",
      "observationpoint": "Larchmont, NY",
      "feelslike": "78",
      "humidity": "62",
      "winddisplay": "5 mph Northwest",
      "day": "Sunday",
      "shortday": "Sun",
      "windspeed": "5 mph",
      "imageUrl": "http://blob.weather.microsoft.com/static/weather4/en-us/law/34.gif"
    },
    "forecast": [
      {
        "low": "68",
        "high": "85",
        "skycodeday": "31",
        "skytextday": "Mostly Clear",
        "date": "2018-07-28",
        "day": "Saturday",
        "shortday": "Sat",
        "precip": ""
      },
      {
        "low": "73",
        "high": "85",
        "skycodeday": "30",
        "skytextday": "Partly Sunny",
        "date": "2018-07-29",
        "day": "Sunday",
        "shortday": "Sun",
        "precip": "0"
      },
      {
        "low": "74",
        "high": "84",
        "skycodeday": "28",
        "skytextday": "Mostly Cloudy",
        "date": "2018-07-30",
        "day": "Monday",
        "shortday": "Mon",
        "precip": "10"
      },
      {
        "low": "76",
        "high": "82",
        "skycodeday": "9",
        "skytextday": "Light Rain",
        "date": "2018-07-31",
        "day": "Tuesday",
        "shortday": "Tue",
        "precip": "80"
      },
      {
        "low": "79",
        "high": "86",
        "skycodeday": "4",
        "skytextday": "T-Storms",
        "date": "2018-08-01",
        "day": "Wednesday",
        "shortday": "Wed",
        "precip": "100"
      }
    ]
  }
]

任何有关如何从中提取特定数据点(例如当前温度)的帮助将不胜感激!

3 个答案:

答案 0 :(得分:0)

您的result是一个包含对象的javascript数组。该数组在索引零处有一个元素。因此,要访问信息,您只需要使用索引和属性名称:

let result  = [{"location": {"name": "Larchmont, NY","lat": "40.928","long": "-73.751","timezone": "-4","alert": "","degreetype": "F","imagerelativeurl": "http://blob.weather.microsoft.com/static/weather4/en-us/"},"current": {"temperature": "78","skycode": "34","skytext": "Mostly Sunny","date": "2018-07-29","observationtime": "11:45:00","observationpoint": "Larchmont, NY","feelslike": "78","humidity": "62","winddisplay": "5 mph Northwest","day": "Sunday","shortday": "Sun","windspeed": "5 mph","imageUrl": "http://blob.weather.microsoft.com/static/weather4/en-us/law/34.gif"},"forecast": [{"low": "68","high": "85","skycodeday": "31","skytextday": "Mostly Clear","date": "2018-07-28","day": "Saturday","shortday": "Sat","precip": ""},{"low": "73","high": "85","skycodeday": "30","skytextday": "Partly Sunny","date": "2018-07-29","day": "Sunday","shortday": "Sun","precip": "0"},{"low": "74","high": "84","skycodeday": "28","skytextday": "Mostly Cloudy","date": "2018-07-30","day": "Monday","shortday": "Mon","precip": "10"},{"low": "76","high": "82","skycodeday": "9","skytextday": "Light Rain","date": "2018-07-31","day": "Tuesday","shortday": "Tue","precip": "80"},{"low": "79","high": "86","skycodeday": "4","skytextday": "T-Storms","date": "2018-08-01","day": "Wednesday","shortday": "Wed","precip": "100"}]}]

console.log(result[0].current.temperature)
console.log(result[0].location.name)

Forecast属性也是一个数组。您可以循环浏览以获取个别日子:

let result  = [{"location": {"name": "Larchmont, NY","lat": "40.928","long": "-73.751","timezone": "-4","alert": "","degreetype": "F","imagerelativeurl": "http://blob.weather.microsoft.com/static/weather4/en-us/"},"current": {"temperature": "78","skycode": "34","skytext": "Mostly Sunny","date": "2018-07-29","observationtime": "11:45:00","observationpoint": "Larchmont, NY","feelslike": "78","humidity": "62","winddisplay": "5 mph Northwest","day": "Sunday","shortday": "Sun","windspeed": "5 mph","imageUrl": "http://blob.weather.microsoft.com/static/weather4/en-us/law/34.gif"},"forecast": [{"low": "68","high": "85","skycodeday": "31","skytextday": "Mostly Clear","date": "2018-07-28","day": "Saturday","shortday": "Sat","precip": ""},{"low": "73","high": "85","skycodeday": "30","skytextday": "Partly Sunny","date": "2018-07-29","day": "Sunday","shortday": "Sun","precip": "0"},{"low": "74","high": "84","skycodeday": "28","skytextday": "Mostly Cloudy","date": "2018-07-30","day": "Monday","shortday": "Mon","precip": "10"},{"low": "76","high": "82","skycodeday": "9","skytextday": "Light Rain","date": "2018-07-31","day": "Tuesday","shortday": "Tue","precip": "80"},{"low": "79","high": "86","skycodeday": "4","skytextday": "T-Storms","date": "2018-08-01","day": "Wednesday","shortday": "Wed","precip": "100"}]}]

result[0].forecast.forEach(forecast => console.log(forecast.date, "low: ", forecast.low, "high: ", forecast.high))

答案 1 :(得分:0)

对于weather-js库无法实现您想要的,因为该库未公开任何其他API,因此只有一个“查找”。

我建议您达到期望的结果是

  • 您可以对库做出很好的贡献,可能只是为您的更改创建请求请求。

  • 您可以在本地环境中拥有一个副本。并添加或编辑结果。

  • 最后一个可以轻松解决您问题的方法就是使用JS转换方法。

weather2.find({
    search: location,
    degreeType: 'F'
}, function (err, result) {
    if (err) console.log(err);
    result = result.map((item) => item.current.temperature);
    console.log(JSON.stringify(result, null, 2));
});

答案 2 :(得分:0)

快速回答:

result[1].current.temperature
// do this so you can access the temperature

嘿,如果你想得到具体的温度,那么这是可能的。 我建议不要将其设为 JSON,因为这无济于事,只会占用内存。 要解释这一点,您需要知道这是一个数组,因此您首先要指定哪个部分。假设为 0,因为您需要用户位置的名称。之后,您执行 .location.name 指定。