これは、政治ですね?(笑・XML派の妨害ですね。)
JSON ってコンパクトですよね。名前なしの{}(中括弧)を使うところが XML 形式より多少なりとも不確実ではあっても記述が短くて済むわけです。
openweathermap サイトのデフォルト設定は JSON 出力です。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"coord":{"lon":-87.65,"lat":41.85},"sys":{"message":1.2238,"country":"US","sunrise":1434017699,"sunset":1434072341},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"base":"stations","main":{"temp":290.857,"temp_min":290.857,"temp_max":290.857,"pressure":1002.52,"sea_level":1024.25,"grnd_level":1002.52,"humidity":74},"wind":{"speed":2.13,"deg":53.5001},"clouds":{"all":48},"rain":{"3h":0.595},"dt":1434048065,"id":4887398,"name":"Chicago","cod":200} |
このサイトは天気情報を提供してくれます。都市名とかを URL で送ってやると天気情報を返してくれます。
そこで、「ねえ、パーサーを書いたら?」とかいう余計な手間をかけたくない忙しいプログラマのため org.json.simple ライブラリの使い方を紹介します。
まず JSON.simple JAR ファイルをダウンロード。
データを読み込むプログラムの手順を示します。
- まず、JSONParser オブジェクトをインスタンス化。
- URL オブジェクトから InputStreamReader、BufferedReader をインスタンス化。
- JSONParser.parse() でパース。
- あとは JSONParser.get() で要素を読んでいくだけです。
- ネストされている要素であれば、JSONParser オブジェクトとして扱う。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WeatherApp.java -- Get Chicago weather from openweathermap site | |
// Copyright (c) 2015 easai | |
// Author: easai | |
// Created: Thu Jun 11 13:23:04 2015 | |
// Keywords: | |
// This file is not part of GNU Emacs. | |
// WeatherApp.java is free software; you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation; either version 2, or (at your option) | |
// any later version. | |
// This software is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// You should have received a copy of the GNU General Public License | |
// along with this program; see the file COPYING. If not, write to the | |
// Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
// Boston, MA 02111-1307, USA. | |
// Commentary: | |
// | |
// | |
// | |
// Code: | |
import java.io.*; | |
import java.net.*; | |
import java.util.*; | |
import org.json.simple.*; | |
import org.json.simple.parser.*; | |
public class WeatherApp | |
{ | |
public static void main(String args[]) | |
{ | |
JSONParser parser=new JSONParser(); | |
try | |
{ | |
URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q=chicago"); | |
try (BufferedReader reader | |
= new BufferedReader(new InputStreamReader(url.openStream()))) | |
{ | |
JSONObject obj = (JSONObject)parser.parse(reader); | |
JSONObject main = (JSONObject)obj.get("main"); | |
double temp = (double)main.get("temp")-273.15; | |
System.out.println(temp); | |
} | |
} | |
catch(Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} | |
// WeatherApp.java ends here |
あとは、JAR ファイルとコンパイルするだけ。
javac -classpath "JARファイルのあるディレクトリ;." WeatherApp.javaこれで、コマンドラインからシカゴの気温が検索できます。