Javaを使用してJSONファイルの内容を読み取る方法は?
JSONまたはJavaScriptObjectNotationは、人間が読める形式のデータ交換用に設計された、軽量のテキストベースのオープンスタンダードです。 JSONで使用される規則は、C、C ++、Java、Python、Perlなどを含むプログラマーに知られています。サンプルJSONドキュメント −
{
"book": [
{
"id": "01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id": "07",
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy"
}
]
} Json-シンプルなライブラリ
json-simpleは、JSONオブジェクトを処理するために使用される軽量ライブラリです。これを使用すると、Javaプログラムを使用してJSONドキュメントのコンテンツを読み書きできます。
JSON-単純なMaven依存関係
以下は、JSON-simpleライブラリのMaven依存関係です-
<dependencies> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> 301 to 305 </dependencies>
これをpom.xmlファイルの最後にある
例
まず、 JSONを作成しましょう sample.jsonという名前のドキュメント 以下に示すように、6つのキーと値のペアを使用します-
{
"ID": "1",
"First_Name": "Shikhar",
"Last_Name": "Dhawan",
"Date_Of_Birth": "1981-12-05",
"Place_Of_Birth":"Delhi",
"Country": "India"
} Javaプログラムを使用してJSONファイルの内容を読み取るには-
- json-simpleライブラリのJSONParserクラスをインスタンス化します。
JSONParser jsonParser = new JSONParser();
- parse()を使用して、取得したオブジェクトのコンテンツを解析します メソッド。
//Parsing the contents of the JSON file
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/players_data.json")); - get()を使用してキーに関連付けられた値を取得します メソッド。
String value = (String) jsonObject.get("key_name"); 次のJavaプログラムは、上記で作成された sample.jsonを解析します ファイル、その内容を読み取り、表示します。
例
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadingJSON {
public static void main(String args[]) {
//Creating a JSONParser object
JSONParser jsonParser = new JSONParser();
try {
//Parsing the contents of the JSON file
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/sample.json"));
String id = (String) jsonObject.get("ID");
String first_name = (String) jsonObject.get("First_Name");
String last_name = (String) jsonObject.get("Last_Name");
String date_of_birth = (String) jsonObject.get("Date_Of_Birth");
String place_of_birth = (String) jsonObject.get("Place_Of_Birth");
String country = (String) jsonObject.get("Country");
//Forming URL
System.out.println("Contents of the JSON are: ");
System.out.println("ID :"+id);
System.out.println("First name: "+first_name);
System.out.println("Last name: "+last_name);
System.out.println("Date of birth: "+date_of_birth);
System.out.println("Place of birth: "+place_of_birth);
System.out.println("Country: "+country);
System.out.println(" ");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
} 出力
Contents of the JSON are: ID :1 First name: Shikhar Last name: Dhawan Date of birth :1981-12-05 Place of birth: Delhi Country: India
-
Javaのプロパティファイルからデータを読み取る方法は?
プロパティ はHashtableクラスのサブクラスであり、プロパティの永続的なセットを表します。 プロパティ ストリームに保存することも、ストリームからロードすることもできます。プロパティリストの各キーとそれに対応する値は文字列です。 プロパティ ファイルをJavaで使用して、構成を外部化し、キーと値のペアを保存できます。 。 Properties.load()メソッド ofPropertiesクラスはロードに便利です。プロパティ key-valueの形式のファイル ペア 。 構文 public class Properties extends Hashtable credential
-
Javaを使用してJSONファイルを作成/作成する方法は?
JSONまたはJavaScriptObjectNotationは、人間が読める形式のデータ交換用に設計された、軽量のテキストベースのオープンスタンダードです。 JSONで使用される規則は、C、C ++、Java、Python、Perlなどを含むプログラマーに知られています。サンプルJSONドキュメント − { "book": [ { "id": "01",