json-simpleの例を日本語で言い換えると以下の通りです:「json-simpleのサンプル」

json-simpleはJSONのための簡単なJavaツールキットです。json-simpleライブラリはJSON仕様(RFC4627)に完全に準拠しています。

json-simpleを日本語で表現すると「シンプルなJSON」です。

json-simpleはJSON処理のために内部的にMapとListを使用します。json-simpleを使用することで、JSONデータのパースやJSONのファイルへの書き込みができます。json-simpleの最も優れた特徴の一つは、第三者のライブラリに依存していないことです。また、json-simpleは非常に軽量なAPIであり、シンプルなJSONの要件に適しています。

以下は”json-simple maven”という英文の日本語での表現です(1つのオプション):
「json-simple maven」

以下の日本語の文における一つの選択肢を用いて言い換えることができます:
json-simpleライブラリーは、こちらからダウンロードしてプロジェクトに追加することができます。json-simpleはmavenセントラルリポジトリで利用可能なので、pom.xmlファイルに依存関係を追加するのが最も適切な方法です。

<dependency>
	<groupId>com.googlecode.json-simple</groupId>
	<artifactId>json-simple</artifactId>
	<version>1.1.1</version>
</dependency>

JSONをファイルに書き込むためのjson-simpleの例

json-simpleのAPIで最も重要なクラスはorg.json.simple.JSONObjectです。JSONObjectのインスタンスを作成し、キーと値のペアを追加します。JSONObjectのtoJSONStringメソッドはJSONを文字列形式で返し、ファイルに書き込むことができます。リストをJSONキーに書き込むためには、org.json.simple.JSONArrayを使用することができます。

package com.scdev.json.write;

import java.io.FileWriter;
import java.io.IOException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class JsonSimpleWriter {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		JSONObject obj = new JSONObject();
		
		obj.put("name", "Pankaj Kumar");
		obj.put("age", new Integer(32));

		JSONArray cities = new JSONArray();
		cities.add("New York");
		cities.add("Bangalore");
		cities.add("San Francisco");

		obj.put("cities", cities);

		try {

			FileWriter file = new FileWriter("data.json");
			file.write(obj.toJSONString());
			file.flush();
			file.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.print(obj.toJSONString());

	}

}

上記のクラスは、data.jsonに書き込みます。下にはこのファイルのJSONコンテンツがあります。

{"cities":["New York","Bangalore","San Francisco"],"name":"Pankaj Kumar","age":32}

mainメソッドには@SuppressWarnings(“unchecked”)アノテーションがあることに気付きましたか?これは、型の安全性に関連する警告を回避するために行われたものです。JSONObjectはHashMapを拡張していますが、ジェネリクスはサポートしていないため、Eclipse IDEは以下のような警告を表示します。

型安全性:メソッドput(Object、Object)は生の型HashMapに属しています。ジェネリック型HashMap<K,V>への参照はパラメータ化されるべきです。

ファイルからJSONを読み取るjson-simpleの例

JSONファイルから読み込みするためには、org.json.simple.parser.JSONParserクラスを使用する必要があります。JSONParserのparseメソッドはJSONObjectを返します。その後、キー名を指定して値を取得できます。以下は、json-simpleを使用したJSONファイルの読み込みの例です。

package com.scdev.json.write;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonSimpleReader {

	public static void main(String[] args) throws ParseException, FileNotFoundException, IOException {
		JSONParser parser = new JSONParser();
		Reader reader = new FileReader("data.json");

		Object jsonObj = parser.parse(reader);

		JSONObject jsonObject = (JSONObject) jsonObj;

		String name = (String) jsonObject.get("name");
		System.out.println("Name = " + name);

		long age = (Long) jsonObject.get("age");
		System.out.println("Age = " + age);

		JSONArray cities = (JSONArray) jsonObject.get("cities");
		
		@SuppressWarnings("unchecked")
		Iterator<String> it = cities.iterator();
		while (it.hasNext()) {
			System.out.println("City = " + it.next());
		}
		reader.close();
	}

}

上記のjson-simpleの例は、以下の出力を生成します。

Name = Pankaj Kumar
Age = 32
City = New York
City = Bangalore
City = San Francisco

json-simpleについての簡単なまとめは以上です。しかし、複雑なJSONデータを扱いたい場合は、JacksonまたはGsonを使用するべきです。また、Java 7に追加されたJSR353も試してみることができます。

コメントを残す 0

Your email address will not be published. Required fields are marked *


广告
広告は10秒後に閉じます。
bannerAds