REST APIを使ってみよう!(3/3)

jqコマンドでREST APIを実行

それでは、インストールしたjqコマンドをパイプで繋ぎ、実行してみます。

sh-3.2# curl "http://weather.livedoor.com/forecast/webservice/json/v1?city=170010" | jq
{
  “pinpointLocations”: [
    {
      "link": "http://weather.livedoor.com/area/forecast/1720100",
      "name": "金沢市"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1720300",
      "name": "小松市"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1720600",
      "name": "加賀市"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1720900",
      "name": "かほく市"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1721000",
      "name": "白山市"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1721100",
      "name": "能美市"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1721200",
      "name": "野々市市"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1732400",
      "name": "川北町"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1736100",
      "name": "津幡町"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1736500",
      "name": "内灘町"
    }
  ],
  "link": "http://weather.livedoor.com/area/forecast/170010",
  "forecasts": [
    {
      "dateLabel": "今日",
      "telop": "晴のち曇",
      "date": "2017-06-06",
      "temperature": {
        "min": null,
        "max": null
      },
      "image": {
        "width": 50,
        "url": "http://weather.livedoor.com/img/icon/5.gif",
        "title": "晴のち曇",
        "height": 31
      }
    },
    {
      "dateLabel": "明日",
      "telop": "曇のち雨",
      "date": "2017-06-07",
      "temperature": {
        "min": {
          "celsius": "18",
          "fahrenheit": "64.4"
        },
        "max": {
          "celsius": "24",
          "fahrenheit": "75.2"
        }
      },
      "image": {
        "width": 50,
        "url": "http://weather.livedoor.com/img/icon/13.gif",
        "title": "曇のち雨",
        "height": 31
      }
    },
    {
      "dateLabel": "明後日",
      "telop": "曇時々雨",
      "date": "2017-06-08",
      "temperature": {
        "min": null,
        "max": null
      },
      "image": {
        "width": 50,
        "url": "http://weather.livedoor.com/img/icon/10.gif",
        "title": "曇時々雨",
        "height": 31
      }
    }
  ],
  "location": {
    "city": "金沢",
    "area": "信越・北陸",
    "prefecture": "石川県"
  },
  "publicTime": "2017-06-06T17:00:00+0900",
  "copyright": {
    "provider": [
      {
        "link": "http://tenki.jp/",
        "name": "日本気象協会"
      }
    ],
    "link": "http://weather.livedoor.com/",
    "title": "(C) LINE Corporation",
    "image": {
      "width": 118,
      "link": "http://weather.livedoor.com/",
      "url": "http://weather.livedoor.com/img/cmn/livedoor.gif",
      "title": "livedoor 天気情報",
      "height": 26
    }
  },
  "title": "石川県 金沢 の天気",
  "description": {
    "text": "(気圧配置など)\n 本州付近は高気圧に覆われています。\n\n(天気分布など)\n 石川県は、晴れ又は薄曇りとなっています。\n\n(今夜の天気 6日)\n 高気圧は次第に日本の東へ移動し、西から気圧の谷が近づいてくる見込み\nです。\n このため、\n 石川県は、晴れで、夜遅くは曇りとなるでしょう。\n\n(明日の天気 7日)\n 低気圧が朝鮮半島から日本海へ進む見込みです。\n このため、\n 石川県は、曇りで、昼前から時々雨となるでしょう。",
    "publicTime”: "2017-06-06T16:46:00+0900”
  }
}
jqコマンドを使用することで、日本語の出力が行えJSONの表示もインデントされました。
さらには文字色もつき、見やすくなりました。

curlではなくJavaScriptや、jQueryから実行及び解析を行うと、ホームページに天気予報を埋め込むことができるのではないでしょうか。
是非皆様も使ってみてください。

前へ
1
2
3

あなたへのおすすめ記事