JSON配列Javaの反復
Java で JSON データを読み書きするには、次を使用します。 org.json 図書館。の org.json ライブラリを使用すると、Java で JSON データをエンコードおよびデコードできるようになります。の org.json class は、JSON データに対していくつかの操作を実行できるいくつかの重要なクラスを提供します。これらのクラスは次のとおりです。
- JSONオブジェクト
- JSON値
- JSON配列
- JSON番号
- JSON文字列
Java で JSON 配列を反復する手順は次のとおりです。
1) を作成します メイビン プロジェクトと追加 json 依存関係 POM.xml ファイル。
org.json json 20160810
2) JSON データの文字列を作成し、それを JSON オブジェクトに変換してそのデータを操作します。
3) その後、次を使用して JSON オブジェクトから JSON 配列を取得します。 getJSONArray() メソッドを作成し、それを JSONArray 型の変数に格納します。
4) getJSONArray() メソッドは、オブジェクトから取得する文字列として配列名を受け取ります。
5) 次に、0 から JSONArray.length() までの for ループを使用します。 length() メソッドは配列のサイズを返します。
6) 各インデックス レコードを JSONObject に保存します。特定のインデックスの JSON オブジェクトを取得するには、JSONArray の getJSONObject() メソッドを使用します。
7) 指定されたフィールドの値を取得するには、JSONObject の get() メソッドを使用し、get() メソッドにフィールド名を文字列として渡します。
JSONExample.java
// import required packages package javaTpoint.JSONExample; //import required classes import org.json.JSONArray; import org.json.JSONObject; // create JSONExample class to iterate JSON Array public class JSONExample{ //main() method start public static void main(String args[]){ // create an string of JSON data String jsonData = '{'locations':[{'lat':'23.053','long':'72.629','location':'ABC','address':'DEF','city':'Ahmedabad','state':'Gujrat','phonenumber':'1234567'},{'lat':'21.013','long':'52.619','location':'XYZ','address':'MNP','city':'Ghaziabad','state':'UP','phonenumber':'212321'}]}'; // convert JSON string into JSON Object using JSONObject() method JSONObject json = new JSONObject(jsonData); // get locations array from the JSON Object and store it into JSONArray JSONArray jsonArray = json.getJSONArray('locations'); // Iterate jsonArray using for loop for (int i = 0; i <jsonarray.length(); i++) { store each object in jsonobject explrobject="jsonArray.getJSONObject(i);" get field value from using get() method system.out.println(explrobject.get('address')); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/10/iterate-json-array-java-2.webp" alt="Iterate JSON Array Java"> <p> <strong>JSONExample2.java</strong> </p> <pre> //import package package javaTpoint.JSONExample; // import require classes import java.io.FileWriter; import java.io.IOException; import org.json.simple.JSONArray; import org.json.simple.JSONObject; // create class JSONExample1 public class JSONExample1 { // main() method start public static void main(String[] args) { // create instance of JSONObject JSONObject jsonObject = new JSONObject(); // use put() method for inserting the data into JSON object jsonObject.put('name', 'Abhishek Sharma'); jsonObject.put('department','B.E'); jsonObject.put('branch', 'C.S.E'); jsonObject.put('year', 3); //create JSON Array and store record into JSONObject JSONArray list1 = new JSONArray(); list1.add('Julia'); list1.add('Paul'); list1.add('Emma'); jsonObject.put('Names', list1); JSONArray list2 = new JSONArray(); list2.add('BCA'); list2.add('B.Tech'); list2.add('MCA'); jsonObject.put('Course', list2); JSONArray list3 = new JSONArray(); list3.add('3'); list3.add('4'); list3.add('2'); jsonObject.put('Year', list3); // create instance of the FileWriter class by passing the path of the file in the constructor try (FileWriter file = new FileWriter('D:\newfile.json')) { // use write() method to add JSONObject into file file.write(jsonObject.toJSONString()); file.flush(); } catch (IOException e) { e.printStackTrace(); } // get locations array from the JSON Object and store it into JSONArray JSONArray jsonArray = (JSONArray) jsonObject.get('Names'); // Iterate jsonArray using for loop for (int i = 0; i <jsonarray.size(); i++) { get field value from json array system.out.println(jsonarray.get(i)); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/10/iterate-json-array-java-3.webp" alt="Iterate JSON Array Java"> <hr></jsonarray.size();></pre></jsonarray.length();>