
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to read/parse 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
{ "books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL] }
The json-simple is a light weight library which is used to process JSON objects. Using this you can read or, write the contents of a JSON document using Java program.
JSON-Simple maven dependency
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 with in the <dependencies> </dependencies> tag at the end of your pom.xml file. (before </project> tag)
Example
First of all, let us create a JSON document with name 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: krishna_kasyap@gmail.com", "phone: 9848022338", "city: Hyderabad", "Area: Madapur", "State: Telangana" ] }
To read an array from a JSON file using a Java program −
- Instantiate the JSONParser class of the json-simple library.
JSONParser jsonParser = new JSONParser();
- Parse the contents of the obtained object using the parse() method.
//Parsing the contents of the JSON file JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/players_data.json"));
- Retrieve the value associated with a key using the get() method.
String value = (String) jsonObject.get("key_name");
- Just like other element retrieve the json array using the get() method into the JSONArray object.
JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
- The iterator() method of the JSONArray class returns an Iterator object using which you can iterate the contents of the current array.
//Iterating the contents of the array Iterator<String> iterator = jsonArray.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); }
Following Java program parses the above created sample.json file, reads its contents and, displays them.
Example
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: krishna_kasyap@gmail.com phone: 9848022338 city: Hyderabad Area: Madapur State: Telangana
- Related Articles
- How to parse JSON in Java?
- How to read data from JSON array using JavaScript?
- How to parse JSON input using Python?
- How to parse a JSON string using Streaming API in Java?
- How to parse JSON on Android using Kotlin?
- How to parse a JSON without duplicate keys using Gson in Java?
- Read by key and parse as JSON in JavaScript
- How to read the contents of a JSON file using Java?
- How to parse JSON Objects on Android using Kotlin?
- How to read volley json array in android?
- How to convert an array to JSON Array using JSON-lib API in Java?\n
- How to convert a JSON array to array using JSON-lib API in Java?\n
- How to parse JSON on Android?
- How to parse a JSON to Gson Tree Model in Java?
- How to use JSONObject to parse JSON in Android using Kotlin?
