Friday, December 27, 2024
Google search engine
HomeLanguagesJavaWorking with JSON Data in Java

Working with JSON Data in Java

JSON stands for JavaScript Object Notation which is a lightweight text-based open standard designed which is easy for human-readable data interchange. In general, JSON is extended from JavaScript. JSON is language-independent and It is easy to read and write. The file extension of JSON is .json.

Example – JSON format 

In the below given example, you will see how you can store values in JSON format. Consider student information where Stu_id, Stu_Name, Course is an entities you need to store then in JSON format you can store these values in key values pair form. Let’s have a look.

{
   "Student": [
    
      {
         "Stu_id"   :  "1001",
         "Stu_Name" :  "Ashish",
         "Course"   :  "Java",
      },
    
      {
         "Stu_id"   :  "1002",
         "Stu_Name" :  "Rana",
         "Course"   :  "Advance Java",
      }
   ]
}

It is the method by which we can access means read or write JSON data in Java Programming Language. Here we simply use the json.simple library to access this feature through Java means we can encode or decode JSON Object using this json.simple library in Java Programming Language. Now, the json.simple package for Java contains the following files in it. So to access we first have to install json.simple package.
 
For installation first, we required to set the json-simple.jar classpath or add the Maven dependency in different cases.

Step 1: Download the json.simple using this link: Download link for json.sample

Step 2: There is one more method to add the Maven dependency, so for that, we have to add the code given below to our pom.xml file.

 <dependency>
    <groupId>com.googlecode.json-simple</groupId>  
    <artifactId>json-simple</artifactId>  
    <version>1.1</version>  
 </dependency>

The above-downloaded .jar file contains these Java source files in it : 

// .jar file 
META-INF/MANIFEST.MF
org.json.simple.ItemList.class
org.json.simple.JSONArray.class
org.json.simple.JSONAware.class
org.json.simple.JSONObject.class
org.json.simple.JSONStreamAware.class
org.json.simple.JSONValue.class
org.json.simple.parser.ContainerFactory.class
org.json.simple.parser.ContentHandler.class
org.json.simple.parser.JSONParser.class
org.json.simple.parser.ParseException.class
org.json.simple.parser.Yylex.class
org.json.simple.parser.Yytoken.class

JSON Object Encoding in Java: As we discussed above, this json.simple library is used to read/write or encode/decode JSON objects in Java. So let’s see how we can code for encoding part of the JSON object using JSONObject function. Now we create a java file mainEncoding.java and save the below-written code in it.

Java




import org.json.simple.JSONObject;
 
// Program for print data in JSON format.
public class JavaJsonEncoding {
    public static void main(String args[])
    {
        // In java JSONObject is used to create JSON object
        // which is a subclass of java.util.HashMap.
 
        JSONObject file = new JSONObject();
 
        file.put("Full Name", "Ritu Sharma");
        file.put("Roll No.", new Integer(1704310046));
        file.put("Tuition Fees", new Double(65400));
 
        // To print in JSON format.
        System.out.print(file);
    }
}


Output :

{"Full Name":"Ritu Sharma", "Roll No.":1704310046, "Tuition Fees":65400}

Now we will see how we can code for decoding part of the JSON object using JSONObjectfunction. Now we create a java file mainDecoding.java and save the below-written code in it. 

Java




import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
 
public class JavaJsonDecoding {
 
    public static void main(String[] args)
    {
        // Converting JSON data into Java String format
        String k = "{\"Full Name\":\"Ritu Sharma\", 
       \"Tuition Fees\":65400.0, \"Roll No.\":1704310046}";
        Object file = JSONValue.parse(k);
 
        // In java JSONObject is used to create JSON object
        JSONObject jsonObjectdecode = (JSONObject)file;
 
        // Converting into Java Data type
        // format From Json is the step of Decoding.
        String name
            = (String)jsonObjectdecode.get("Full Name");
        double fees
            = (Double)jsonObjectdecode.get("Tuition Fees");
        long rollno
            = (Long)jsonObjectdecode.get("Roll No.");
        System.out.println(name + " " + fees + " "
                           + rollno);
    }
}


Output :

Ritu Sharma 65400.0 1704310046

Note: Here Java JSON Encoding can also be done using a list or map

RELATED ARTICLES

Most Popular

Recent Comments