Parse JSON in java
In this post, you will learn how to parse the JSON in the Java programming language.
JSON (JavaScript Object Notation) is popular among developers for data serialization. It is a data format consisting of key-value pairs. It is widely used to transmit and receive data between a server and a web application in JSON format. It is so popular that every modern programming language has methods to generate and parse JSON formatted data.
A common use of JSON is to exchange data to/from a web server. JSON parsing is the process of converting a JSON object in text format to a Java object that can be used inside a program.
Java Parse JSON
To read data from a JSON document using Java, we first need to instantiate the JSONObject class of the json-simple library.
//Creating a JSONObject object
JSONObject jsonObject = new JSONObject();
The JSONObject class provides the get() method to get the required key value.
jsonObject.get("key");
We can easily parse JSON data received from the web server using the JSONParser() method. This method parses a JSON string and constructs the Java value or object described by the string.
Object obj = new JSONParser().parse(new FileReader());
In the given Java program, we read the JSON data from the JSON file "demo.json" with the help of JSONParser, JSONObject and JSONArray.
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.*;
public class FirstProgram {
public static void main(String[] args) throws Exception {
// parsing file "JSONExample.json"
Object obj = new JSONParser().parse(new FileReader("c://java/demo.json"));
// typecasting obj to JSONObject
JSONObject jsnObj = (JSONObject) obj;
// getting firstName and lastName
String firstName = (String) jsnObj.get("FirstName");
String lastName = (String) jsnObj.get("LastName");
System.out.println("FirstName: " + firstName);
System.out.println("LastName: " + lastName);
// getting age
long age = (long) jsnObj.get("Age");
System.out.println("Age: " + age);
// getting address
Map address = ((Map)jsnObj.get("Address"));
// iterating address Map
Iterator<Map.Entry> itr1 = address.entrySet().iterator();
while (itr1.hasNext()) {
Map.Entry pair = itr1.next();
System.out.println(pair.getKey() + " : " + pair.getValue());
}
// getting MobileNumbers
JSONArray jsnArr = (JSONArray) jsnObj.get("MobileNumber");
// iterating phoneNumbers
Iterator itr2 = jsnArr.iterator();
while (itr2.hasNext())
{
itr1 = ((Map) itr2.next()).entrySet().iterator();
while (itr1.hasNext()) {
Map.Entry pair = itr1.next();
System.out.println(pair.getKey() + " : " + pair.getValue());
}
}
}
}
Output of the above code:
FirstName: Henry
LastName: Goldy
Age: 35
State : Corsica
Street : 25 FR 4th Street
PostalCode : 20199
City : Ajaccio
number : 33-15-4233
type : home
Related Articles
Parse JSON in JavaNumber pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number