Read JSON file in Java
To know about reading a JSON file in Java first we must know about the JSON file.
JSON:
JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and sending data. It is used when the data is transporting from server to Webpage. It is used in the field of “Web Development”.
The creating an object in JavaScript is like creating a JSON format as the JSON is originated from the JavaScript.
- The JSON is text file so it can transfer easily.
- The JSON doesn’t depend upon the specific language.
Syntax:
The data should be in the format of name, value pairs and the various data are separated by commas. The curly brackets are used to store objects and square brackets are used to store arrays.
Features of JSON:
Following are some features of the JSON:
- Simple.
- Platform Independent.
- Easy to transfer.
- Extensibility.
- Interoperability.
Datatypes:
Following are some of the datatypes:
- String – A String is represented inside the quotation marks.
- Number- It represents Numeric characters.
- Boolean – It consists of either true or False.
- Null – Empty
JSON In Java:
To use the JSON in java we must use the “json.simple” library to encode and decode. To execute the JSON program a “json.simple” jar must be install and set class path. The data structures used in JSON are:
- JSON objects
- JSON Arrays
In Detail:
- JSON Objects:
The JSON objects are represented in between the curly brackets. The objects should be in the Key/value pairs. The Key is represented as String and Values is represented any Datatypes which are mentioned above.
Example:
Key, value pairs – {“Name”: “Kotte”}
- JSON Arrays:
The JSON Arrays are used to store the objects. These objects are enclosed in square brackets[].
Example:
[{
“Name” : ”Kotte”,
“College” : ”BVRIT”
“Branch” : “Computer Science Engineering”,
“Section” : “CSE_C”
},
{
“Name” : ”Saikiran”,
“College” : ”BVRIT”
“Branch” : “Computer Science Engineering”,
“Section” : “CSE_C”
}]
The above example shows the Student details as array and inside the array the data of students are stored as objects.
A simple program for JSON in Java:
import org.json.simple.JSONObject;
public class Json
{
public static void main(String args[])
{
JSONObject j = new JSONObject();
j.put(“Name”, “Kotte”);
j.put(“College”, “BVRIT”);
j.put(“Branch” , “Computer science engineering”);
j.put(“Section”, “CSE-C”);
System.out.println(j);
}
}
Output:
{“Name”: “Kotte”, “College” : “BVRIT”, “Branch” : “Computer Science Engineering”, “Section” : “CSE-C”}
Reading JSON File in Java:
To read the JSON file in Java, “FileReader()” method is used to read the given JSON file.
Example:
{
“name” : “Kotte”,
“college” : “BVRIT”
}
The above code is the file which is used to read. we use the “json.simple” library.
//program for reading a JSON file
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.*;
public class JSON
{
public static void main(Strings args[])
{
// file name is File.json
Object o = new JSONParser().parse(new FileReader(File.json));
JSONObject j = (JSONObject) o;
String Name = (String) j.get(“Name”);
String College = (String ) j.get(“College”);
System.out.println(“Name :” + Name);
System.out.println(“College :” +College);
}
}
Output:
Name: Kotte
College: BVRIT
In the above program, the JSONParser().parse() is used which is present in the “org.json.simple.parser.*” to parse the File.json file.