Java JSON Example

In this instructional exercise, you will find out about Java JSON example.

What is JSON?

  • JSON represents JavaScript Object Notation.
  • JSON is a lightweight and simpler route for trading information on the web.
  • JSON is begun from JavaScript programming language.
  • JSON is a decent option of XML.
  • JSON is language free and supports information structures like object and cluster.
Java JSON Example

Java JSON Example

To utilize JSON with Java we require a library called as json.simple. You can download it from underneath connect.

In the wake of downloading it, you have to import it to the IDE (NetBeans, Eclipse, and so on) you are utilizing.

Encode and Decode JSON object in Java

The JSON object contains information as key and worth pair. A JSON object example is given beneath.

{"name":"Kishan Kaushik","age":28}

An example to encode and decipher JSON object in Java is given beneath.

import org.json.simple.JSONObject;
 
public class JavaJsonExample {
	public static void main(String args[]) {
		
		//creating JSON object
		JSONObject obj=new JSONObject();
		
		//Encode JSON Object
		obj.put("name","Kishan Kaushik");
		obj.put("age",new Integer(28));
		
		//Decode JSON Object
		System.out.println("Name:"+obj.get("name"));
		System.out.println("Age:"+obj.get("age"));
	}
}

Output:

Kishan Kaushik
28

Encode and Decode JSON Array in Java

A JSON cluster example is given beneath.

["C","C++","Java","Python"]

An example to encode and decipher JSON exhibit in Java is given underneath.

import org.json.simple.JSONArray;
 
public class JavaJsonExample {
	public static void main(String args[]) {
		
		//creating JSON Array
		JSONArray ar=new JSONArray();
		
		//Encode JSON Array
		ar.add("C");
		ar.add("C++");
		ar.add("Java");
		ar.add("Python");
		
		//Decode JSON Array
		for(int i=0;i<ar.size();++i) {
			System.out.println(ar.get(i));			
		}
	}
}

Output

C
C++
Java
Python

Encode and Decode JSON Array of Objects in Java

A JSON cluster of objects example is given beneath.

[{"name":"C"},{"name":"C++"},{"name":"Java"}]

An example to encode and decipher JSON exhibit of objects in Java is given underneath.

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
 
public class JavaJsonExample {
	public static void main(String args[]) {
		
		//creating JSON Array
		JSONArray ar=new JSONArray();
 
		JSONObject obj;
		
		//Creating and adding first JSON Object to JSON Array
		obj=new JSONObject();
		obj.put("name","C");
		ar.add(obj);
		
		//Creating and adding second JSON Object to JSON Array
		obj=new JSONObject();
		obj.put("name","C++");
		ar.add(obj);
 
		//Creating and adding third JSON Object to JSON Array
		obj=new JSONObject();
		obj.put("name","Java");
		ar.add(obj);
 
		
		//Retrieving JSON Objects from JSON Array
		for(int i=0;i<ar.size();++i) {
			obj=(JSONObject)ar.get(i);
			System.out.println(obj.get("name"));			
		}
	}
}

Output

C
C++
Java

Decipher JSON String in Java

We can decipher the JSON string in the following manner.

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
 
public class JavaJsonExample {
	public static void main(String args[]) {
		String JSONObjectString="{\"name\":\"Kishan Kaushik\"}";
		String JSONArrayString="[\"C\",\"C++\",\"Java\"]";
 
		JSONObject obj=(JSONObject)JSONValue.parse(JSONObjectString);
		JSONArray ar=(JSONArray)JSONValue.parse(JSONArrayString);
		
		System.out.println(obj);
		System.out.println(ar);
 
	}
}

Output:

{“name”:”Kishan Kaushik”}
[“C”,”C++”,”Java”]

The above Java JSON example is plain as day, still on the off chance that you can’t see, then don’t hesitate to ask by remarking beneath.

Leave a Comment

error: Alert: Content is protected!!