Iterate JSON array in Java
This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array.
Json Array
Ordered lists of values are represented by JSON arrays. Multiple values can be stored in a JSON array. In a JSON array, you can store a string, a number, a boolean, or an object.Objects in a JSON array must be distinguished by commas.JSON array is represented by the [] (square bracket).
The below is a simple example for the Json array
{"employees":[
{"name":"Edmund", "place":"Rome", "age":23},
{"name":"Horatio", "place":"Iltaly", "age":28},
{"name":"Malacchai", "place":"Portland", "age":33},
{"name":"Aurora", "place":"England", "age":41}
]}
Java uses the org.json package to access and manipulate JSON data. JSON data can be encoded and decoded in Java using the org.json package. The org.json class provides a number of crucial classes that allow us to execute various actions on the JSON data. The main classes are
- JSONObject
- JSONValue
- JSONArray
- JSONNumber
- JSONString
JSONObject
An irreversible JSON object variable (an apparently random collection of none or maybe more name/value pairs) is represented by the JsonObject class. The JSON object name/value conversions are also provided in an immutable map view.
Using JsonReader, a JsonObject instances can be produced from a source of input.
readObject().
JSONValue
An object, array, numeric, text, boolean (true or false), or null is an example of a JSON value. Except for objects and arrays, all values are linear. SQL considers a JSON result of null to be a value.
JSONArray
An irreversible JSON array is represented by sonArray (an ordered sequence of zero or more values). Additionally, it offers a static list view of the array's values.
Either applying an array constructor object or importing JSON data from an input source, a JsonArray object can be formed.
The example that follows shows how to use the JsonReader.readArray() function to build a JsonArray object from an input source.
JSONNumber
Deployments may locally record the numeric value in a BigDecimal object. The int, long, BigInteger, double, and String types are all able to create the BigDecimal object. The BigDecimal interpretations are used to define several of the procedure definitions in this class.
JSONString
Researchers most often use JSON (JavaScript Object Notation), a lightweight data-exchange format, for consumer interactions. It is both simple to read and write and merge.
How it Works
The following are the steps of how to iterate the JSON array in Java
- Make a Maven installation and include a POM.xml file with a json dependency.
- Create a JSON data string that you can manipulate by turning it into a JSON object.
- Next, we use the getJSONArray() method to extract the JSON Array from the JSON Object and save it in a variable with the type JSONArray.
- The array name is a string that is retrieved from the Object using the getJSONArray() function.
- After that, a for loop from 0 to JSONArray is used.
- The array's size is returned via the length() method.
- Each index record is kept as a JSONObject. We utilise the getJSONObject() function of the JSONArray to retrieve the JSON object associated with a specific index.
- By supplying the global variable as a string to the get() method of the JSONObject, we may retrieve the value of a specific field.
Let’s understand with an example program.
File Name: JSONArr.java
package javaTpoint.JSONExample;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JSONArr {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "Manoj Tomar");
jsonObject.put("dept","Engineering");
jsonObject.put("branch", "C.S.E");
jsonObject.put("academic year", 3);
JSONArray list1 = new JSONArray();
list1.add("Ravi");
list1.add("Rajesh");
list1.add("Raashi");
jsonObject.put("Names", list1);
JSONArray list2 = new JSONArray();
list2.add("JAVA");
list2.add("CPP");
list2.add("DBMS");
jsonObject.put("Course", list2);
JSONArray list3 = new JSONArray();
list3.add("3");
list3.add("4");
list3.add("2");
jsonObject.put("Year", list3);
try (FileWriter file = new FileWriter("D:\\newfile.json")) {
file.write(jsonObject.toJSONString());
file.flush();
}
catch (IOException e) {
e.printStackTrace();
}
JSONArray jsonArray = (JSONArray) jsonObject.get("Names");
for (int i = 0; i < jsonArray.size(); i++) {
System.out.println(jsonArray.get(i));
}
}
}
Output
Ravi
Rajesh
Raashi
Distinction with Array
A data structure called an array is used by most computer languages and holds a collection of variables in some kind of a predetermined sequence. An array data type exists in JSON.
The sample would involve a JSON data structure displaying the same content if the initial line and the semicolon at the end were removed. This is due to the fact that JSON is built on a portion of JavaScript, as Its already stated.