3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Ruby]コピペでOpenWeatherMap

Last updated at Posted at 2019-06-07

OpenWeatherMapからAPIで天気情報を取得する*ターミナル実行版とwebアプリ(Rails)実装版

OpenWeatherMapの登録は下記参照
https://qiita.com/nownabe/items/aeac1ce0977be963a740

ターミナル実行版

OpenWeatherMap.rb

API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
BASE_URL = "https://api.openweathermap.org/data/2.5/forecast/"

require "json"
require "open-uri"

response = open(BASE_URL + "?id=1859171&APPID=#{"xxxxxxxxxxxxxxxxxxxxxxxxxxx"}")
puts JSON.pretty_generate(JSON.parse(response.read))

ターミナルで実行
[*注意]ruby -v '2.6.0'以上だとエラーになる

ターミナル
$ ruby OpenWeatherMap.rb                          //実行

実行結果

{
  "cod": "200",
  "message": 0.0073,
  "cnt": 40,
  "list": [
    {
      "dt": 1559887200,
      "main": {
        "temp": 296.86,
        "temp_min": 296.86,
        "temp_max": 301.4,
        "pressure": 1002.13,
        "sea_level": 1002.13,
        "grnd_level": 980.88,
        "humidity": 67,
        "temp_kf": -4.54
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 94
      },
      "wind": {
        "speed": 4.82,
        "deg": 213.786
      },
      "rain": {
        "3h": 1.376
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-07 06:00:00"
    },
    {
      "dt": 1559898000,
      "main": {
        "temp": 294.7,
        "temp_min": 294.7,
        "temp_max": 298.1,
        "pressure": 1001.03,
        "sea_level": 1001.03,
        "grnd_level": 980.11,
        "humidity": 80,
        "temp_kf": -3.4
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 100
      },
      "wind": {
        "speed": 2.71,
        "deg": 213.267
      },
      "rain": {
        "3h": 0.188
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-07 09:00:00"
    },
    {
      "dt": 1559908800,
      "main": {
        "temp": 294.43,
        "temp_min": 294.43,
        "temp_max": 296.7,
        "pressure": 1001.62,
        "sea_level": 1001.62,
        "grnd_level": 980.95,
        "humidity": 68,
        "temp_kf": -2.27
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 100
      },
      "wind": {
        "speed": 5.25,
        "deg": 249.352
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-07 12:00:00"
    },
    {
      "dt": 1559919600,
      "main": {
        "temp": 293.37,
        "temp_min": 293.37,
        "temp_max": 294.5,
        "pressure": 1001.61,
        "sea_level": 1001.61,
        "grnd_level": 980.99,
        "humidity": 69,
        "temp_kf": -1.13
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 100
      },
      "wind": {
        "speed": 5.86,
        "deg": 252.839
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-07 15:00:00"
    },
    {
      "dt": 1559930400,
      "main": {
        "temp": 292.8,
        "temp_min": 292.8,
        "temp_max": 292.8,
        "pressure": 1000.95,
        "sea_level": 1000.95,
        "grnd_level": 980.71,
        "humidity": 75,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 100
      },
      "wind": {
        "speed": 5.69,
        "deg": 248.281
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-07 18:00:00"
    },
    {
      "dt": 1559941200,
      "main": {
        "temp": 292.3,
        "temp_min": 292.3,
        "temp_max": 292.3,
        "pressure": 1001.49,
        "sea_level": 1001.49,
        "grnd_level": 980.83,
        "humidity": 82,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "clouds": {
        "all": 45
      },
      "wind": {
        "speed": 5.06,
        "deg": 241.092
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-07 21:00:00"
    },
    {
      "dt": 1559952000,
      "main": {
        "temp": 296.5,
        "temp_min": 296.5,
        "temp_max": 296.5,
        "pressure": 1002.88,
        "sea_level": 1002.88,
        "grnd_level": 981.36,
        "humidity": 62,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 58
      },
      "wind": {
        "speed": 3.98,
        "deg": 249.845
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-08 00:00:00"
    },
    {
      "dt": 1559962800,
      "main": {
        "temp": 296.2,
        "temp_min": 296.2,
        "temp_max": 296.2,
        "pressure": 1003.73,
        "sea_level": 1003.73,
        "grnd_level": 981.81,
        "humidity": 70,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 67
      },
      "wind": {
        "speed": 1.5,
        "deg": 340.308
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-08 03:00:00"
    },
    {
      "dt": 1559973600,
      "main": {
        "temp": 295.4,
        "temp_min": 295.4,
        "temp_max": 295.4,
        "pressure": 1004.25,
        "sea_level": 1004.25,
        "grnd_level": 982.75,
        "humidity": 72,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 72
      },
      "wind": {
        "speed": 4.61,
        "deg": 357.365
      },
      "rain": {
        "3h": 0.438
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-08 06:00:00"
    },
    {
      "dt": 1559984400,
      "main": {
        "temp": 293.048,
        "temp_min": 293.048,
        "temp_max": 293.048,
        "pressure": 1004.99,
        "sea_level": 1004.99,
        "grnd_level": 983.92,
        "humidity": 76,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "clouds": {
        "all": 31
      },
      "wind": {
        "speed": 4.5,
        "deg": 0.42
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-08 09:00:00"
    },
    {
      "dt": 1559995200,
      "main": {
        "temp": 289.7,
        "temp_min": 289.7,
        "temp_max": 289.7,
        "pressure": 1006.06,
        "sea_level": 1006.06,
        "grnd_level": 985.13,
        "humidity": 88,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03n"
        }
      ],
      "clouds": {
        "all": 42
      },
      "wind": {
        "speed": 2.18,
        "deg": 6.09
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-08 12:00:00"
    },
    {
      "dt": 1560006000,
      "main": {
        "temp": 289.72,
        "temp_min": 289.72,
        "temp_max": 289.72,
        "pressure": 1006.28,
        "sea_level": 1006.28,
        "grnd_level": 985.69,
        "humidity": 86,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 83
      },
      "wind": {
        "speed": 1.76,
        "deg": 334.496
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-08 15:00:00"
    },
    {
      "dt": 1560016800,
      "main": {
        "temp": 288.042,
        "temp_min": 288.042,
        "temp_max": 288.042,
        "pressure": 1005.9,
        "sea_level": 1005.9,
        "grnd_level": 984.97,
        "humidity": 92,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 57
      },
      "wind": {
        "speed": 1.71,
        "deg": 359.597
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-08 18:00:00"
    },
    {
      "dt": 1560027600,
      "main": {
        "temp": 289.247,
        "temp_min": 289.247,
        "temp_max": 289.247,
        "pressure": 1006.09,
        "sea_level": 1006.09,
        "grnd_level": 985.07,
        "humidity": 86,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 87
      },
      "wind": {
        "speed": 2.01,
        "deg": 356.186
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-08 21:00:00"
    },
    {
      "dt": 1560038400,
      "main": {
        "temp": 294.176,
        "temp_min": 294.176,
        "temp_max": 294.176,
        "pressure": 1006.33,
        "sea_level": 1006.33,
        "grnd_level": 985.33,
        "humidity": 63,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 56
      },
      "wind": {
        "speed": 3.34,
        "deg": 17.913
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-09 00:00:00"
    },
    {
      "dt": 1560049200,
      "main": {
        "temp": 295.8,
        "temp_min": 295.8,
        "temp_max": 295.8,
        "pressure": 1005.59,
        "sea_level": 1005.59,
        "grnd_level": 984.65,
        "humidity": 59,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 69
      },
      "wind": {
        "speed": 4.09,
        "deg": 35.192
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-09 03:00:00"
    },
    {
      "dt": 1560060000,
      "main": {
        "temp": 298.405,
        "temp_min": 298.405,
        "temp_max": 298.405,
        "pressure": 1004.47,
        "sea_level": 1004.47,
        "grnd_level": 983.01,
        "humidity": 53,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "clouds": {
        "all": 44
      },
      "wind": {
        "speed": 2.69,
        "deg": 34.783
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-09 06:00:00"
    },
    {
      "dt": 1560070800,
      "main": {
        "temp": 294.41,
        "temp_min": 294.41,
        "temp_max": 294.41,
        "pressure": 1004.04,
        "sea_level": 1004.04,
        "grnd_level": 982.98,
        "humidity": 72,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 31
      },
      "wind": {
        "speed": 4,
        "deg": 19.591
      },
      "rain": {
        "3h": 0.25
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-09 09:00:00"
    },
    {
      "dt": 1560081600,
      "main": {
        "temp": 289.351,
        "temp_min": 289.351,
        "temp_max": 289.351,
        "pressure": 1005.35,
        "sea_level": 1005.35,
        "grnd_level": 984.46,
        "humidity": 89,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 801,
          "main": "Clouds",
          "description": "few clouds",
          "icon": "02n"
        }
      ],
      "clouds": {
        "all": 20
      },
      "wind": {
        "speed": 2.87,
        "deg": 11.416
      },
      "rain": {
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-09 12:00:00"
    },
    {
      "dt": 1560092400,
      "main": {
        "temp": 288.4,
        "temp_min": 288.4,
        "temp_max": 288.4,
        "pressure": 1005.21,
        "sea_level": 1005.21,
        "grnd_level": 984.18,
        "humidity": 90,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01n"
        }
      ],
      "clouds": {
        "all": 6
      },
      "wind": {
        "speed": 2.52,
        "deg": 7.258
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-09 15:00:00"
    },
    {
      "dt": 1560103200,
      "main": {
        "temp": 287.939,
        "temp_min": 287.939,
        "temp_max": 287.939,
        "pressure": 1004.63,
        "sea_level": 1004.63,
        "grnd_level": 983.37,
        "humidity": 94,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01n"
        }
      ],
      "clouds": {
        "all": 5
      },
      "wind": {
        "speed": 2.32,
        "deg": 12.261
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-09 18:00:00"
    },
    {
      "dt": 1560114000,
      "main": {
        "temp": 289.358,
        "temp_min": 289.358,
        "temp_max": 289.358,
        "pressure": 1005.1,
        "sea_level": 1005.1,
        "grnd_level": 983.94,
        "humidity": 90,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01d"
        }
      ],
      "clouds": {
        "all": 7
      },
      "wind": {
        "speed": 2.3,
        "deg": 16.687
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-09 21:00:00"
    },
    {
      "dt": 1560124800,
      "main": {
        "temp": 296.343,
        "temp_min": 296.343,
        "temp_max": 296.343,
        "pressure": 1005.53,
        "sea_level": 1005.53,
        "grnd_level": 984.13,
        "humidity": 66,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01d"
        }
      ],
      "clouds": {
        "all": 4
      },
      "wind": {
        "speed": 2.04,
        "deg": 45.815
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-10 00:00:00"
    },
    {
      "dt": 1560135600,
      "main": {
        "temp": 298.408,
        "temp_min": 298.408,
        "temp_max": 298.408,
        "pressure": 1005,
        "sea_level": 1005,
        "grnd_level": 983.49,
        "humidity": 63,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 12
      },
      "wind": {
        "speed": 1.79,
        "deg": 21.528
      },
      "rain": {
        "3h": 2.125
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-10 03:00:00"
    },
    {
      "dt": 1560146400,
      "main": {
        "temp": 296.257,
        "temp_min": 296.257,
        "temp_max": 296.257,
        "pressure": 1004.36,
        "sea_level": 1004.36,
        "grnd_level": 983.06,
        "humidity": 76,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 501,
          "main": "Rain",
          "description": "moderate rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 7
      },
      "wind": {
        "speed": 3.57,
        "deg": 21.13
      },
      "rain": {
        "3h": 6.437
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-10 06:00:00"
    },
    {
      "dt": 1560157200,
      "main": {
        "temp": 293.947,
        "temp_min": 293.947,
        "temp_max": 293.947,
        "pressure": 1005.03,
        "sea_level": 1005.03,
        "grnd_level": 983.77,
        "humidity": 84,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 501,
          "main": "Rain",
          "description": "moderate rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 13
      },
      "wind": {
        "speed": 3.2,
        "deg": 21.107
      },
      "rain": {
        "3h": 3.75
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-10 09:00:00"
    },
    {
      "dt": 1560168000,
      "main": {
        "temp": 290.3,
        "temp_min": 290.3,
        "temp_max": 290.3,
        "pressure": 1006.84,
        "sea_level": 1006.84,
        "grnd_level": 985.56,
        "humidity": 90,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10n"
        }
      ],
      "clouds": {
        "all": 8
      },
      "wind": {
        "speed": 1.98,
        "deg": 7.495
      },
      "rain": {
        "3h": 0.188
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-10 12:00:00"
    },
    {
      "dt": 1560178800,
      "main": {
        "temp": 289.445,
        "temp_min": 289.445,
        "temp_max": 289.445,
        "pressure": 1007.43,
        "sea_level": 1007.43,
        "grnd_level": 986.32,
        "humidity": 92,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01n"
        }
      ],
      "clouds": {
        "all": 0
      },
      "wind": {
        "speed": 1.61,
        "deg": 13.709
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-10 15:00:00"
    },
    {
      "dt": 1560189600,
      "main": {
        "temp": 288.941,
        "temp_min": 288.941,
        "temp_max": 288.941,
        "pressure": 1007.49,
        "sea_level": 1007.49,
        "grnd_level": 986.05,
        "humidity": 92,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01n"
        }
      ],
      "clouds": {
        "all": 0
      },
      "wind": {
        "speed": 0.95,
        "deg": 22.657
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-10 18:00:00"
    },
    {
      "dt": 1560200400,
      "main": {
        "temp": 291.4,
        "temp_min": 291.4,
        "temp_max": 291.4,
        "pressure": 1008.18,
        "sea_level": 1008.18,
        "grnd_level": 986.81,
        "humidity": 88,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01d"
        }
      ],
      "clouds": {
        "all": 0
      },
      "wind": {
        "speed": 0.7,
        "deg": 67.893
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-10 21:00:00"
    },
    {
      "dt": 1560211200,
      "main": {
        "temp": 296.4,
        "temp_min": 296.4,
        "temp_max": 296.4,
        "pressure": 1008.44,
        "sea_level": 1008.44,
        "grnd_level": 987.3,
        "humidity": 67,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01d"
        }
      ],
      "clouds": {
        "all": 1
      },
      "wind": {
        "speed": 1.97,
        "deg": 174.658
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-11 00:00:00"
    },
    {
      "dt": 1560222000,
      "main": {
        "temp": 299.559,
        "temp_min": 299.559,
        "temp_max": 299.559,
        "pressure": 1007.41,
        "sea_level": 1007.41,
        "grnd_level": 986.23,
        "humidity": 50,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 6
      },
      "wind": {
        "speed": 2.13,
        "deg": 214.981
      },
      "rain": {
        "3h": 1.062
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-11 03:00:00"
    },
    {
      "dt": 1560232800,
      "main": {
        "temp": 299.7,
        "temp_min": 299.7,
        "temp_max": 299.7,
        "pressure": 1006.16,
        "sea_level": 1006.16,
        "grnd_level": 984.58,
        "humidity": 46,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 801,
          "main": "Clouds",
          "description": "few clouds",
          "icon": "02d"
        }
      ],
      "clouds": {
        "all": 20
      },
      "wind": {
        "speed": 3.15,
        "deg": 221.401
      },
      "rain": {
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-11 06:00:00"
    },
    {
      "dt": 1560243600,
      "main": {
        "temp": 296.559,
        "temp_min": 296.559,
        "temp_max": 296.559,
        "pressure": 1006.38,
        "sea_level": 1006.38,
        "grnd_level": 985.28,
        "humidity": 65,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 95
      },
      "wind": {
        "speed": 1.96,
        "deg": 223.261
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-11 09:00:00"
    },
    {
      "dt": 1560254400,
      "main": {
        "temp": 292.7,
        "temp_min": 292.7,
        "temp_max": 292.7,
        "pressure": 1007.86,
        "sea_level": 1007.86,
        "grnd_level": 986.74,
        "humidity": 79,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 97
      },
      "wind": {
        "speed": 1.2,
        "deg": 222.469
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-11 12:00:00"
    },
    {
      "dt": 1560265200,
      "main": {
        "temp": 291.041,
        "temp_min": 291.041,
        "temp_max": 291.041,
        "pressure": 1007.44,
        "sea_level": 1007.44,
        "grnd_level": 986.36,
        "humidity": 87,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 77
      },
      "wind": {
        "speed": 0.8,
        "deg": 212.856
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-11 15:00:00"
    },
    {
      "dt": 1560276000,
      "main": {
        "temp": 290.141,
        "temp_min": 290.141,
        "temp_max": 290.141,
        "pressure": 1006.83,
        "sea_level": 1006.83,
        "grnd_level": 985.68,
        "humidity": 92,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03n"
        }
      ],
      "clouds": {
        "all": 39
      },
      "wind": {
        "speed": 0.43,
        "deg": 109.654
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2019-06-11 18:00:00"
    },
    {
      "dt": 1560286800,
      "main": {
        "temp": 292.2,
        "temp_min": 292.2,
        "temp_max": 292.2,
        "pressure": 1007.35,
        "sea_level": 1007.35,
        "grnd_level": 986.15,
        "humidity": 84,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01d"
        }
      ],
      "clouds": {
        "all": 0
      },
      "wind": {
        "speed": 0.64,
        "deg": 102.032
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-11 21:00:00"
    },
    {
      "dt": 1560297600,
      "main": {
        "temp": 297.3,
        "temp_min": 297.3,
        "temp_max": 297.3,
        "pressure": 1007.36,
        "sea_level": 1007.36,
        "grnd_level": 986.29,
        "humidity": 64,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01d"
        }
      ],
      "clouds": {
        "all": 0
      },
      "wind": {
        "speed": 1.85,
        "deg": 157.755
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-12 00:00:00"
    },
    {
      "dt": 1560308400,
      "main": {
        "temp": 299.986,
        "temp_min": 299.986,
        "temp_max": 299.986,
        "pressure": 1006.59,
        "sea_level": 1006.59,
        "grnd_level": 985.33,
        "humidity": 55,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "clear sky",
          "icon": "01d"
        }
      ],
      "clouds": {
        "all": 2
      },
      "wind": {
        "speed": 1.6,
        "deg": 241.438
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2019-06-12 03:00:00"
    }
  ],
  "city": {
    "id": 1859171,
    "name": "Kobe-shi",
    "coord": {
      "lat": 34.6913,
      "lon": 135.183
    },
    "country": "JP",
    "timezone": 32400
  }
}

webアプリ(Rails)実装版

weather.js
$(document).ready(function() {
  $.post("http://api.openweathermap.org/data/2.5/weather?id=" + '1850147' + "&appid=xxxxxxxxxxxxxxxxxxxxxxxxxxx&lang=ja&units=metric",
      function(json){
          $("#weather").html(json.weather[0].description);
          $("#humidity").html(json.main.humidity);
          $("#temp").html(Math.round(json.main.temp));
          switch (json.weather[0].main){
          case 'Clouds':
          $("#weatherMark").html("<img src='http://openweathermap.org/img/w/04d.png' >");
          break;
          case 'Snow':
          $("#weatherMark").html("<img src='http://openweathermap.org/img/w/13d.png' >");
          break;
          case 'Rain':
          $("#weatherMark").html("<img src='http://openweathermap.org/img/w/09d.png' >");
          break;
          case 'Clear':
          $("#weatherMark").html("<img src='http://openweathermap.org/img/w/01d.png' >");
          break;
          case 'Fog':
          $("#weatherMark").html("<img src='http://openweathermap.org/img/w/50d.png' >");
          break;
          case 'Mist':
          $("#weatherMark").html("<img src='http://openweathermap.org/img/w/50n.png' >");
          break;
          case 'Haze':
          $("#weatherMark").html("<img src='http://openweathermap.org/img/w/50d.png' >");
          break;
          default:
          $("#weatherMark").html("<img src='http://openweathermap.org/img/w/01n.png' >");
        }
    }
  );
});

_weather.html
<h2>東京の天気</h2>
<div>
    <span id = "weather" class="bold"></span>
</div>
<div>
    <span id = "weatherMark" class="bold"></span>
</div>
<p id="icon"></p>
<div>気温<span id = "temp"  class="bold"></span></div>
<div>降水確率<span id = "humidity" class="bold"></span> %</div>

参考サイト

http://www.torutsume.net/openweathermap/
https://qiita.com/nownabe/items/aeac1ce0977be963a740

告知

今、進撃のITコミュニティで一緒に活動してくれる方を募集しています。
参加は無料です。
下に参加URLを貼ってます。
管理者はブロックチェーン実務経験あります。
参加資格はプログラマーからマーケター、AIやブロックチェーン、動画編集者やディレクター、youtuber、インスタグラマーも対象です。
進撃のIT.png

進撃のIT Facebook
https://www.facebook.com/groups/612023275874253/
進撃のIT Slack
https://attack-on-it.herokuapp.com/
進撃のIT Twitter
https://twitter.com/IT13389135
進撃のIT Qiita記事
https://qiita.com/f___juntaro_/items/81136c85a8002cc442ac

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?