Javatpoint Logo
Javatpoint Logo

Iterate JSON Array Java

In order to read and write JSON data in Java, we use org.json library. The org.json library allow us to encode and decode JSON data in Java. The org.json class provide several important classes through which we can perform several operations on that JSON data. These classes are as follows:

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

These are the following steps to iterate JSON array in Java:

1) Create a Maven project and add json dependency in POM.xml file.


Iterate JSON Array Java

2) Create a string of JSON data which we convert into JSON object to manipulate its data.

3) After that, we get the JSON Array from the JSON Object using getJSONArray() method and store it into a variable of type JSONArray.

4) The getJSONArray() method takes array name as a string to retrieve from the Object.

5) We then use a for loop from 0 to JSONArray.length(). The length() method return the size of the array .

6) We store each index record into a JSONObject. In order to get the JSON object of a particular index, we use getJSONObject() method of JSONArray.

7) To get a value of a specified field, we use the get() method of the JSONObject by passing the field name as a string in the get() method.

JSONExample.java

Output:

Iterate JSON Array Java

JSONExample2.java

Output:

Iterate JSON Array Java
Next TopicJava Date Add Days





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA