In this tutorial you will learn about Java JSON example.
What is JSON?
- JSON stands for JavaScript Object Notation.
- JSON is a lightweight and an easier way for exchanging data on the web.
- JSON is originated from JavaScript programming language.
- JSON is a good alternative of XML.
- JSON is language independent and supports data structures like object and array.
Java JSON Example
To use JSON with Java we require a library called as json.simple. You can download it from below link.
Download: http://www.javatpoint.com/jsonpages/json-simple-1.1.1.jar
After downloading it you need to import it to the IDE (NetBeans, Eclipse, etc) you are using.
Encode and Decode JSON object in Java
JSON object contains data in the form of key and value pair. A JSON object example is given below.
{"name":"Neeraj Mishra","age":21}
An example to encode and decode JSON object in Java is given below.
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","Neeraj Mishra"); obj.put("age",new Integer(21)); //Decode JSON Object System.out.println("Name:"+obj.get("name")); System.out.println("Age:"+obj.get("age")); } }
Output
Neeraj Mishra
21
Encode and Decode JSON Array in Java
A JSON array example is given below.
["C","C++","Java","Python"]
An example to encode and decode JSON array in Java is given below.
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 array of objects example is given below.
[{"name":"C"},{"name":"C++"},{"name":"Java"}]
An example to encode and decode JSON array of objects in Java is given below.
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
Decode JSON String in Java
We can decode JSON string in following way.
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\":\"Neeraj Mishra\"}"; 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”:”Neeraj Mishra”}
[“C”,”C++”,”Java”]
The above Java JSON example is self explanatory, still if you are unable to understand then feel free to ask by commenting below.