Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read/parse JSON array using Java?
In this article, we will learn to read/parse a JSON array using Java. A JSON array is an ordered collection of values that are enclosed in square brackets, i.e., it begins with ?[' and ends with ?]'. The values in the arrays are separated by ?,' (comma).
Sample JSON array
The following is the syntax for JSON array initialization:
{
books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL]
}
Reading (Parsing) JSON array in Java
The following are the different approaches for reading/parsing a JSON array in Java:
JSON-Simple maven dependency
The JSON-simple is a lightweight library that is used to process JSON objects. Using this, you can read or write the contents of a JSON document using a Java program.
The following is the Maven dependency for the JSON-simple library:
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
Paste this within the <dependencies> </dependencies> tag at the end of your pom.xml file. (before </project> tag)
Using the Jackson Library
Jackson is a fast library for working with JSON in Java. It's good for big or complex projects that need speed and powerful features.
The Following is the Jackson dependency for reading/parsing a JSON array:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
Using the Gson Library
Gson is a JSON library from Google. It's easy to use and works well for small projects where simplicity matters more than speed.
The Following is the Gson dependency for reading/parsing a JSON array:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
Reading/Parsing JSON array
First of all, let us create a JSON document named sample.json with the 6 key-value pairs and an array as shown below ?
{
"ID": "1",
"First_Name": "Krishna Kasyap",
"Last_Name": "Bhagavatula",
"Date_Of_Birth": "1989-09-26",
"Place_Of_Birth":"Vishakhapatnam",
"Salary": "25000"
"contact": [
"e-mail: [email protected]",
"phone: 9848022338",
"city: Hyderabad",
"Area: Madapur",
"State: Telangana"
]
}
Instantiate the JSONParser class of the json-simple library:
JSONParser jsonParser = new JSONParser();
Parse the contents of the obtained object using the parse() method:
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/players_data.json"));
Retrieve the value associated with a key using the get() method:
JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
The iterator() method of the JSONArray class returns an Iterator object, using which you can iterate through the contents of the current array.
Iterator<String> iterator = jsonArray.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
Example
Below is an example of a Java program that parses the above-created sample.json file, reads its contents, and displays them.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadingArrayFromJSON {
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:/test.json"));
//Forming URL
System.out.println("Contents of the JSON are: ");
System.out.println("ID: "+jsonObject.get("ID"));
System.out.println("First name: "+jsonObject.get("First_Name"));
System.out.println("Last name: "+jsonObject.get("Last_Name"));
System.out.println("Date of birth: "+ jsonObject.get("Date_Of_Birth"));
System.out.println("Place of birth: "+ jsonObject.get("Place_Of_Birth"));
System.out.println("Salary: "+jsonObject.get("Salary"));
//Retrieving the array
JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
System.out.println("");
System.out.println("Contact details: ");
//Iterating the contents of the array
Iterator<String> iterator = jsonArray.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output
Contents of the JSON are: ID: 1 First name: Krishna Kasyap Last name: Bhagavatula Date of birth: 1989-09-26 Place of birth: Vishakhapatnam Salary: 25000 Contact details: e-mail: [email protected] phone: 9848022338 city: Hyderabad Area: Madapur State: Telangana