Org-json-quick-guide
org.json-概要
*org.jsonまたはJSON-Java* は、JSON用のシンプルなJavaベースのツールキットです。 org.jsonを使用して、JSONデータをエンコードまたはデコードできます。
特徴
- 仕様準拠-JSON.simpleはJSON仕様-RFC4627に完全に準拠しています。
- 軽量-クラスは非常に少なく、エンコード/デコードやjsonのエスケープなどの必要な機能を提供します。
- * XML変換*-JSONからXMLへ、またはその逆への変換機能を提供します。
- * HTTPヘッダー*-JSONへのHTTPヘッダー変換、およびその逆のサポート。
- Cookie -JSONへのCookie変換、およびその逆のサポートを提供します。
- CDL -カンマ区切りリストをJSONに、またはその逆に変換するサポートを提供します。
- 依存関係なし-外部ライブラリの依存関係なし。 独立して含めることができます。
- * Java 1.6-1.8互換*-ソースコードとバイナリはJava 1.6-1.8互換です
org.json-環境設定
ローカル環境のセットアップ
JSON.simpleはJavaのライブラリなので、最初の要件はマシンにJDKをインストールすることです。
システム要件
JDK | 1.5 or above. |
Memory | No minimum requirement. |
Disk Space | No minimum requirement. |
Operating System | No minimum requirement. |
ステップ1:マシンでのJavaインストールの検証
まず、コンソールを開き、作業しているオペレーティングシステムに基づいてjavaコマンドを実行します。
OS | Task | Command |
---|---|---|
Windows | Open Command Console | c:\> java -version |
Linux | Open Command Terminal | $ java -version |
Mac | Open Terminal | machine:< joseph$ java -version |
すべてのオペレーティングシステムの出力を確認しましょう-
OS | Output |
---|---|
Windows |
java version "1.8.0_101" Java(TM)SEランタイム環境(ビルド1.8.0_101) |
Linux |
java version "1.8.0_101" Java(TM)SEランタイム環境(ビルド1.8.0_101) |
Mac |
java version "1.8.0_101" Java(TM)SEランタイム環境(ビルド1.8.0_101) |
システムにJavaがインストールされていない場合は、次のリンクからJavaソフトウェア開発キット(SDK)をダウンロードしてください。https://www.oracle.com/technetwork/java/javase/downloads/indexl[www.oracle.com ]。 このチュートリアルのインストールバージョンとしてJava 1.8.0_101を想定しています。
ステップ2:JAVA環境を設定する
*JAVA_HOME* 環境変数を設定して、Javaがマシンにインストールされているベースディレクトリの場所を指すようにします。 例えば。
OS | Output |
---|---|
Windows | Set the environment variable JAVA_HOME to C:\Program Files\Java\jdk1.8.0_101 |
Linux | export JAVA_HOME =/usr/local/java-current |
Mac | export JAVA_HOME =/Library/Java/Home |
Javaコンパイラの場所をシステムパスに追加します。
OS | Output |
---|---|
Windows | Append the string C:\Program Files\Java\jdk1.8.0_101\bin *at the end of the system variable, Path*. |
Linux | export PATH = $PATH:$JAVA_HOME/bin/ |
Mac | not required |
上記の説明に従って、コマンド java -version を使用してJavaのインストールを確認します。
ステップ3:org.jsonアーカイブをダウンロードする
org.json @ MVNRepositoryからorg.json jarファイルの最新バージョンをダウンロードします。 このチュートリアルを書いている時点で、json-20180813をダウンロードし、C:\> JSONフォルダーにコピーしました。
OS | Archive name |
---|---|
Windows | json-20180813.jar |
Linux | json-20180813.jar |
Mac | json-20180813.jar |
ステップ4:JSON_JAVA環境を設定する
*JSON_JAVA* 環境変数を設定して、マシン上のorg.json jarが保存されているベースディレクトリの場所を指定します。 json-20180813.jarをJSONフォルダーに保存したと仮定します。
Sr.No | OS & Description |
---|---|
1 |
Windows 環境変数JSON_JAVAをC:\ JSONに設定します |
2 |
Linux エクスポートJSON_JAVA =/usr/local/JSON |
3 |
Mac エクスポートJSON_JAVA =/Library/JSON |
ステップ5:CLASSPATH変数を設定する
JSON.simple jarの場所を指すように CLASSPATH 環境変数を設定します。
Sr.No | OS & Description |
---|---|
1 |
Windows 環境変数CLASSPATHを%CLASSPATH%;%JSON_JAVA%\ json-20180813.jar;。;に設定します |
2 |
Linux export CLASSPATH = $ CLASSPATH:$ JSON_JAVA/json-20180813.jar :. |
3 |
Mac export CLASSPATH = $ CLASSPATH:$ JSON_JAVA/json-20180813.jar :. |
org.json-CDL
CDLクラスは、コンマ区切りのテキストをJSONArrayに、またはその逆に変換する静的メソッドを提供します。
この例では、次の方法について説明します。
- * rowToJSONArray(String)*-コンマ区切りテキストをJSONArrayオブジェクトに変換します。
- * rowToString(JSONArray)*-JSONArrayをコンマ区切りテキストに変換します。
- * toJSONArray(String)*-複数行のコンマ区切りテキストをJSONArrayオブジェクトのオブジェクトに変換します。
- * toJSONArray(JSONArray、String)*-JSONArrayオブジェクトとコンマ区切りテキストをJSONArrayオブジェクトに変換します。
例
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONTokener;
public class JSONDemo {
public static void main(String[] args) {
String csvData = "INDIA, UK, USA";
//Case 1: CSV to JSON Array
JSONArray jsonArray = CDL.rowToJSONArray(new JSONTokener(csvData));
System.out.println(jsonArray);
//Case 2: JSONArray to CSV
System.out.println(CDL.rowToString(jsonArray));
//Case 3: CSV to JSONArray of Objects
csvData = "empId, name, age \n" +
"1, Mark, 22 \n" +
"2, Robert, 35 \n" +
"3, Julia, 18";
System.out.println(CDL.toJSONArray(csvData));
//Case 4: CSV without header
jsonArray = new JSONArray();
jsonArray.put("empId");
jsonArray.put("name");
jsonArray.put("age");
csvData = "1, Mark, 22 \n" + "2, Robert, 35 \n" + "3, Julia, 18";
System.out.println(CDL.toJSONArray(jsonArray,csvData));
}
}
出力
["INDIA","UK","USA"]
INDIA,UK,USA
[{"name":"Mark","empId":"1","age":"22"},
{"name":"Robert","empId":"2","age":"35"},
{"name":"Julia","empId":"3","age":"18"}]
[{"name":"Mark","empId":"1","age":"22"},
{"name":"Robert","empId":"2","age":"35"},
{"name":"Julia","empId":"3","age":"18"}]
org.json-クッキー
Cookieクラスは、WebブラウザーのCookieテキストをJSONObjectに、またはその逆に変換する静的メソッドを提供します。
この例では、次の方法について説明します。
- * toJSONObject(String)*-CookieテキストをJSONObjectオブジェクトに変換します。
- * toString(JSONObject)*-JSONObjectをCookieテキストに変換します。
例
import org.json.Cookie;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
String cookie = "username = Mark Den; expires = Thu, 15 Jun 2020 12:00:00 UTC; path =/";
//Case 1: Converts Cookie String to JSONObject
JSONObject jsonObject = Cookie.toJSONObject(cookie);
System.out.println(jsonObject);
//Case 2: Converts JSONObject to Cookie String
System.out.println(Cookie.toString(jsonObject));
}
}
出力
{"path":"/","expires":"Thu, 15 Jun 2020 12:00:00 UTC","name":"username","value":"Mark Den"}
username=Mark Den;expires=Thu, 15 Jun 2020 12:00:00 UTC;path=/
org.json-CookieList
CookieListクラスは、CookieリストをJSONObjectに、またはその逆に変換する静的メソッドを提供します。 Cookieリストは、名前と値のペアのシーケンスです。
この例では、次の方法について説明します。
- * toJSONObject(String)*-CookieリストのテキストをJSONObjectオブジェクトに変換します。
- * toString(JSONObject)*-JSONObjectをCookieリストテキストに変換します。
例
import org.json.Cookie;
import org.json.CookieList;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
String cookie = "username = Mark Den; expires = Thu, 15 Jun 2020 12:00:00 UTC; path =/";
//Case 1: Converts Cookie String to JSONObject
JSONObject cookieJSONObject = Cookie.toJSONObject(cookie);
JSONObject cookielistJSONObject = new JSONObject();
cookielistJSONObject.put(cookieJSONObject.getString("name"),
cookieJSONObject.getString("value"));
String cookieList = CookieList.toString(cookielistJSONObject);
System.out.println(cookieList);
System.out.println(CookieList.toJSONObject(cookieList));
}
}
出力
username=Mark Den
{"username":"Mark Den"}
org.json-HTTP
HTTPクラスは、WebブラウザーのヘッダーテキストをJSONObjectに、またはその逆に変換する静的メソッドを提供します。
この例では、次の方法について説明します。
- * toJSONObject(String)*-ヘッダーテキストをJSONObjectオブジェクトに変換します。
- * toString(JSONObject)*-JSONObjectをヘッダーテキストに変換します。
例
import org.json.HTTP;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("Method", "POST");
jsonObject.put("Request-URI", "http://www.finddevguides.com/");
jsonObject.put("HTTP-Version", "HTTP/1.1");
//Case 1: Converts JSONObject of Header to String
String headerText = HTTP.toString(jsonObject);
System.out.println(headerText);
headerText = "POST \"http://www.finddevguides.com/\" HTTP/1.1";
//Case 2: Converts Header String to JSONObject
System.out.println(HTTP.toJSONObject(headerText));
}
}
出力
POST "http://www.finddevguides.com/" HTTP/1.1
{"Request-URI":"http://www.finddevguides.com/","Method":"POST","HTTP-Version":"HTTP/1.1"}
org.json-JSONArray
JSONArrayは、値の順序付けられたシーケンスです。 インデックスによって値にアクセスし、値を書き込むメソッドを提供します。 次のタイプがサポートされています-
- ブール値
- JSONArray
- JSONObject
- 数
- ひも
- JSONObject.NULLオブジェクト
例
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
JSONArray list = new JSONArray();
list.put("foo");
list.put(new Integer(100));
list.put(new Double(1000.21));
list.put(new Boolean(true));
list.put(JSONObject.NULL);
System.out.println("JSONArray: ");
System.out.println(list);
}
}
出力
JSONArray:
["foo",100,1000.21,true,null]
org.json-JSONML
JSONMLクラスは、XMLテキストをJSONArrayに、またはその逆に変換する静的メソッドを提供します。
この例では、次の方法について説明します。
- * toJSONArray(String)*-XMLをJSONArrayオブジェクトに変換します。
- * toJSONObject(String)*-XMLをJSONObjectオブジェクトに変換します。
- * toString(JSONArray)*-JSONArrayオブジェクトからXMLを提供します。
- * toString(JSONObject)*-JSONObjectオブジェクトからXMLを提供します。
例
import org.json.JSONArray;
import org.json.JSONML;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
JSONArray list = new JSONArray();
list.put("name");
list.put("Robert");
System.out.println("XML from a JSONArray: ");
String xml = JSONML.toString(list);
System.out.println(xml);
System.out.println("JSONArray from a XML: ");
list = JSONML.toJSONArray(xml);
System.out.println(list);
System.out.println("JSONObject from a XML: ");
JSONObject object = JSONML.toJSONObject(xml);
System.out.println(object);
System.out.println("XML from a JSONObject: ");
xml = JSONML.toString(object);
System.out.println(xml);
}
}
出力
XML from a JSONArray:
<name>Robert</name>
JSONArray from a XML:
["name","Robert"]
JSONObject from a XML:
{"childNodes":["Robert"],"tagName":"name"}
XML from a JSONObject:
<name>Robert</name>
org.json-JSONObject
JSONObjectクラスは、キーと値のペアの順不同のコレクションです。 キーによって値にアクセスし、値を入力するメソッドを提供します。 次のタイプがサポートされています-
- ブール値
- JSONArray
- JSONObject
- 数
- ひも
- JSONObject.NULLオブジェクト
例
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("Name", "Robert");
jsonObject.put("ID", 1);
jsonObject.put("Fees", new Double(1000.21));
jsonObject.put("Active", new Boolean(true));
jsonObject.put("Other Details", JSONObject.NULL);
JSONArray list = new JSONArray();
list.put("foo");
list.put(new Integer(100));
jsonObject.put("list",list);
System.out.println(jsonObject);
}
}
出力
{"Active":true,"Other Details":null,"ID":1,"Fees":1000.21,"list":["foo",100],"Name":"Robert"}
org.json-JSONStringer
JSONStringerは、JSON構文規則を確認するJSONテキストをすばやく作成するユーティリティクラスです。 JSONStringerの各インスタンスは、1つのJSONテキストを生成できます。
例
import org.json.JSONStringer;
public class JSONDemo {
public static void main(String[] args) {
String jsonText = new JSONStringer()
.object()
.key("Name")
.value("Robert")
.endObject()
.toString();
System.out.println(jsonText);
jsonText = new JSONStringer()
.array()
.value("Robert")
.value("Julia")
.value("Dan")
.endArray()
.toString();
System.out.println(jsonText);
jsonText = new JSONStringer()
.array()
.value("Robert")
.value("Julia")
.value("Dan")
.object()
.key("Name")
.value("Robert")
.endObject()
.endArray()
.toString();
System.out.println(jsonText);
}
}
出力
{"Name":"Robert"}
["Robert","Julia","Dan"]
["Robert","Julia","Dan",{"Name":"Robert"}]
org.json-プロパティ
プロパティクラスは、プロパティテキストをJSONObjectに、またはその逆に変換する静的メソッドを提供します。
この例では、次の方法について説明します。
- * toJSONObject(Properties)*-プロパティデータをJSONObjectオブジェクトに変換します。
- * toProperties(JSONObject)*-JSONObjectをプロパティオブジェクトに変換します。
例
import java.util.Properties;
import org.json.JSONObject;
import org.json.Property;
public class JSONDemo {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("title", "This is a title text");
properties.put("subtitle", "This is a subtitle text");
System.out.println("Properties to JSON");
JSONObject jsonObject = Property.toJSONObject(properties);
System.out.println(jsonObject);
System.out.println("JSON to properties");
System.out.println(Property.toProperties(jsonObject));
}
}
出力
Properties to JSON
{"subtitle":"This is a subtitle text","title":"This is a title text"}
JSON to properties
{subtitle = This is a subtitle text, title = This is a title text}
org.json-XML
XMLクラスは、XMLテキストをJSONObjectに、またはその逆に変換する静的メソッドを提供します。
この例では、次の方法について説明します。
- * toJSONObject(String)*-XMLをJSONArrayオブジェクトに変換します。
- * toString(JSONObject)*-JSONObjectオブジェクトからXMLを提供します。
例
import org.json.JSONObject;
import org.json.XML;
public class JSONDemo {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("Name", "Robert");
jsonObject.put("ID", 1);
jsonObject.put("Fees", new Double(1000.21));
jsonObject.put("Active", new Boolean(true));
jsonObject.put("Details", JSONObject.NULL);
//Convert a JSONObject to XML
String xmlText = XML.toString(jsonObject);
System.out.println(xmlText);
//Convert an XML to JSONObject
System.out.println(XML.toJSONObject(xmlText));
}
}
出力
<Active>true</Active><Details>null</Details><ID>1</ID><Fees>1000.21</Fees><Name>Robert</Name>
{"Active":true,"Details":null,"ID":1,"Fees":1000.21,"Name":"Robert"}
org.json-JSONException処理
org.jsonのユーティリティクラスは、無効なJSONの場合にJSONExceptionをスローします。 次の例は、JSONExceptionを処理する方法を示しています。
例
import org.json.JSONException;
import org.json.XML;
public class JSONDemo {
public static void main(String[] args) {
try{
//XML tag name should not have space.
String xmlText = "<Other Details>null</Other Details>";
System.out.println(xmlText);
//Convert an XML to JSONObject
System.out.println(XML.toJSONObject(xmlText));
} catch(JSONException e){
System.out.println(e.getMessage());
}
}
}
出力
position: 24
Unexpected token RIGHT BRACE(}) at position 24.