חזר על JSON Array Java

חזר על JSON Array Java

על מנת לקרוא ולכתוב נתוני JSON ב-Java, אנו משתמשים org.json סִפְרִיָה. ה org.json הספרייה מאפשרת לנו לקודד ולפענח נתוני JSON ב-Java. ה org.json class מספקים מספר מחלקות חשובות שדרכן נוכל לבצע מספר פעולות על נתוני JSON אלה. שיעורים אלה הם כדלקמן:

  1. JSONObject
  2. JSONValue
  3. JSONArray
  4. JSONNumber
  5. JSONString

אלו הם השלבים הבאים לחזרה על מערך JSON ב-Java:

1) צור א מייבן פרוייקט ולהוסיף json תלות ב POM.xml קוֹבֶץ.

 org.json json 20160810  

חזר על JSON Array Java

2) צור מחרוזת של נתוני JSON אותה אנו ממירים לאובייקט JSON כדי לתפעל את הנתונים שלו.

3) לאחר מכן, אנו מקבלים את מערך JSON מ-JSON Object באמצעות getJSONArray() שיטה ואחסן אותו במשתנה מסוג JSONArray.

4) ה getJSONArray() השיטה לוקחת את שם המערך כמחרוזת לשליפה מהאובייקט.

5) לאחר מכן אנו משתמשים בלולאת for מ-0 ל-JSONArray.length(). שיטת length() מחזירה את גודל המערך.

6) אנו מאחסנים כל רשומת אינדקס ב-JSONObject. על מנת לקבל את אובייקט JSON של אינדקס מסוים, אנו משתמשים בשיטת getJSONObject() של JSONArray.

7) כדי לקבל ערך של שדה מוגדר, אנו משתמשים בשיטת get() של ה-JSONObject על ידי העברת שם השדה כמחרוזת בשיטת 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 = &apos;{&apos;locations&apos;:[{&apos;lat&apos;:&apos;23.053&apos;,&apos;long&apos;:&apos;72.629&apos;,&apos;location&apos;:&apos;ABC&apos;,&apos;address&apos;:&apos;DEF&apos;,&apos;city&apos;:&apos;Ahmedabad&apos;,&apos;state&apos;:&apos;Gujrat&apos;,&apos;phonenumber&apos;:&apos;1234567&apos;},{&apos;lat&apos;:&apos;21.013&apos;,&apos;long&apos;:&apos;52.619&apos;,&apos;location&apos;:&apos;XYZ&apos;,&apos;address&apos;:&apos;MNP&apos;,&apos;city&apos;:&apos;Ghaziabad&apos;,&apos;state&apos;:&apos;UP&apos;,&apos;phonenumber&apos;:&apos;212321&apos;}]}&apos;; // 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(&apos;locations&apos;); // 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(&apos;name&apos;, &apos;Abhishek Sharma&apos;); jsonObject.put(&apos;department&apos;,&apos;B.E&apos;); jsonObject.put(&apos;branch&apos;, &apos;C.S.E&apos;); jsonObject.put(&apos;year&apos;, 3); //create JSON Array and store record into JSONObject JSONArray list1 = new JSONArray(); list1.add(&apos;Julia&apos;); list1.add(&apos;Paul&apos;); list1.add(&apos;Emma&apos;); jsonObject.put(&apos;Names&apos;, list1); JSONArray list2 = new JSONArray(); list2.add(&apos;BCA&apos;); list2.add(&apos;B.Tech&apos;); list2.add(&apos;MCA&apos;); jsonObject.put(&apos;Course&apos;, list2); JSONArray list3 = new JSONArray(); list3.add(&apos;3&apos;); list3.add(&apos;4&apos;); list3.add(&apos;2&apos;); jsonObject.put(&apos;Year&apos;, list3); // create instance of the FileWriter class by passing the path of the file in the constructor try (FileWriter file = new FileWriter(&apos;D:\newfile.json&apos;)) { // 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(&apos;Names&apos;); // 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();>