処理としては、以下の記事の続きになります。 Eclipse + JFace : HttpURLConnection で GET ※ Google gson のダウンロードはこちら ※ Google gson の実装はこちら ※ オンラインドキュメントはこちら まずは、JSON データとして WEB API を Livedoor の お天気Webサービスを使用します。地域id を指定するだけで、その地域の天気データを取得できる API です。 ▼ 大阪 http://weather.livedoor.com/forecast/webservice/json/v1?city=270000 JSON のチェックと整形は JSONLint サービスでどうぞGoogle gson 用のクラス定義 JSON のフォーマットに合わせて定義しますが、順序を合わす必要は無く、名前(変数名)を一致させるようにします。サンプルでは、文字列と整数を使っていますが、JsonElement で可能な変換はできると思います。 Getter と Setter でアプリケーションに必要な変換をそこで定義してしまってもいいと思います。
public class Weather {
class PinpointLocation {
String link;
String name;
}
class Text {
String text;
String publicTime;
}
class Location {
String city;
String area;
String prefecture;
}
class IntTest {
ImageSize image;
}
class ImageSize {
int width;
int height;
}
PinpointLocation[] pinpointLocations;
Location location;
IntTest copyright;
Text description;
// Getter と Setter で処理する
private String publicTime;
String getPublicTime() {
return publicTime.substring(0, 10);
}
void setPublicTime(String publicTime) {
this.publicTime = publicTime;
}
}
実行
/**
* Create contents of the application window.
* @param parent
*/
@Override
protected Control createContents(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(null);
{
Button btnNewButton = new Button(container, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("ボタンの処理");
HttpGet hg = new HttpGet();
String result =
hg.execute(
"http://weather.livedoor.com/forecast/webservice/json/v1?city=270000",
"utf-8"
);
System.out.println(result);
Gson gson = new Gson();
Weather json = gson.fromJson(result,Weather.class);
// 内部クラスの変数として定義されたものをそのまま利用
System.out.println(json.location.city);
System.out.println(json.location.area);
System.out.println(json.location.prefecture);
System.out.println(json.description.text);
System.out.println(json.description.publicTime);
// Setter でセットされたものを Getter で取得
System.out.println(json.getPublicTime());
// クラスの配列として定義していたものを使用
for( int i = 0; i < json.pinpointLocations.length; i++ ) {
System.out.println(json.pinpointLocations[i].link);
System.out.println(json.pinpointLocations[i].name);
}
// 数値変数
System.out.println(json.copyright.image.width);
System.out.println(json.copyright.image.height);
}
});
btnNewButton.setBounds(10, 10, 81, 28);
btnNewButton.setText("New Button");
}
return container;
}
|
|
【Javaの最新記事】
- Java : Apache POI で最低限の処理を標準化( ここではワークブックは新規作成 )
- Java で JSON 文字列を オブジェクトに変換する Google Gson の基本 4 パターン
- Java のコンソールで NAVER の RSS を取得して、title 部分を正規表現で加工して表示するサンプル
- Eclipse で、文字列内のファイルのパスの \ 記号を \\ にする方法
- Java : Google gson 2.3.1 で、JSON 文字列のフォーマットが解らなくてもなんとかなる『ベタ』な処理方法
- Eclipse のホバーで追加したライブラリの javadoc を表示させる手順
- Eclipse+WindowBuilder : DBアプリケーション(社員マスタメンテ)の更新(データ修正)処理
- Eclipse+WindowBuilder : DBアプリケーション(社員マスタメンテ)の入力チェック
- Eclipse+WindowBuilder : DBアプリケーション(社員マスタメンテ)の二会話画面制御
- Eclipse+WindowBuilder : DBアプリケーション(社員マスタメンテ)でDBからSELECT構文で行データを読み出す
- Eclipse+WindowBuilder : ボタンイベント(AbstractAction) の作成
- Eclipse+WindowBuilder : データベースアプリケーション(社員マスタメンテ)の画面作成
- Eclipse + WindowBuilder : JDBC と ODBC を使った、オールマイティなデータベース接続サンプル( MySQL / SQLServer / Oracle / Postgr..
- Eclipse + WindowBuilder : Design タブが表示されなくなった時の対処
- Eclipse のパンくずリスト(breadcrumb) をワークスペースの設定ファイルで非表示にする
- Java : Eclipse 実行の System.in.read(buff) でコンソール入力
- iText( itextpdf-5.4.3 / Java ) で簡単に PDF 出力をする。
- SQL 文へのデータバインド用 public class GetSQL
- ADT( Android Development Tools ) Eclipse に Window Builder をインストールして Swing アプリケーションを作成する(2)
- Java : Windows の環境変数の列挙( 含ソート )


Google gson 用のクラス定義
JSON のフォーマットに合わせて定義しますが、順序を合わす必要は無く、名前(変数名)を一致させるようにします。サンプルでは、文字列と整数を使っていますが、



