How can we read a JSON file in Java?
Source: DZone JSON is a simple format for storing and sending data to a web page. It is commonly used in JavaScript, but today we will learn how to work with it in Java.Reading a JSON file in Java
To learn about reading a JSON file in Java, first we need to understand what a JSON file is. JSON is an acronym for “JavaScript Object Notation”. JSON can be used to send data from a server to a web page. This means that it is used in web development. It is important to remember:- JSON is a text file, so it can be easily transferred.
- JSON is not language specific.
Syntax
The data in the JSON file must be in the format of name/value pairs, with commas separating the different data. Curly braces are used to store objects, and square brackets are used to store arrays.JSON Features
Below are some features of JSON:- Simple.
- Has an independent platform.
- Easy to convey.
- Supports extensibility.
- Availability of compatibility.
Data types
Data types specific to JSON:- String - The string is represented inside quotes.
- Number - Represents numeric characters.
- Boolean – consists of either true or false.
- Null – empty.
JSON in Java
To use JSON in Java, we must use the json.simple library for encoding and decoding. To run the JSON program and set the classpath, you need to install the jar (Java archive) json.simple. Data structures used in JSON:- JSON objects;
- JSON arrays.
JSON objects
JSON objects are represented between curly braces. Objects must be in key/value pairs. The key is represented as a String and the values represent any of the data types mentioned above. Example:Key, value pairs - {"Name": "Kotte"}
JSON arrays
JSON arrays are used to store 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"
}]
In the above example, the student details are represented as an array and inside the array, the student data is stored as objects.
Simple JSON Program 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);
}
}
Conclusion:
{"Name": "Kotte", "College": "BVRIT", "Branch": "Computer Science Engineering", "Section": "CSE-C"}
Reading a JSON file in Java
To read a JSON file in Java, you need to use the FileReader() method. Example:{
"name" : "Kotte",
"college" : "BVRIT"
}
The above code is the file that is used to read. We are using 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);
}
}
Conclusion:
Name: Kotte College: BVRIT
This program uses JSONParser().parse() which is present in org.json.simple.parser.* to parse the File.json file.
What is Java Development Kit (JDK)?
Source: Medium This post will introduce you to the workings and uses of the Java Development Kit. In their work, programmers often use the Java Development Kit, known as the JDK. It is a cross-platform set of tools and libraries for creating Java-based software applications and applets. The JDK includes both the Java Virtual Machine (known as the JVM) and the Java Runtime Environment (known as the JRE). The JDK also includes the javac compiler, performance monitoring tools, a debugger, standard Java class libraries, examples, documentation, and various utilities. The Java Development Kit (JDK) is developed by Oracle Corporation. Its product implements JVMS, JLS and Java API SE (API). In addition to the commercial version, Oracle introduced a free version of the OpenJDK package to the market. There are also alternative JDKs from other companies in the Java development market. If the only thing you want to do on your computer is run Java applications, then you don't need to worry about the differences between the Java Runtime Environment (JRE) and the Java Development Kit (JDK). However, you will need the Java Development Kit (JDK) to create Java-based software. The Java Runtime Environment (JRE) included with the JDK is called the Private Runtime. This environment differs from the standard JRE in that it includes additional components. It also gives developers access to the Java Virtual Machine (JVM) and all class libraries used in production environments, in addition to internationalization and IDL libraries.Most Popular JDKs
In addition to JDK and OpenJDK from Oracle, developers should pay attention to other versions of the package:- Azul Systems Zing : A high-performance, low-latency Java virtual machine for Linux.
- Azul Systems (for Linux, Windows, Mac OS X and IBM J9 JDK: for AIX, Linux, Windows and many other operating systems).
- Amazon Corretto (OpenJDK and long-term support included).
GO TO FULL VERSION