About JSON structure and Java
Posted By : Kiran Sharma | 27-Dec-2018
INTRODUCTION
In this blog i am going to describe JSON data creation and representation with the help of Java programming. JSON is a light weight text based data exchange format. It is easly understandable by humans and machines. It is independent of any language. It stands for JavaScript Object Notation (JSON). JSON file is stored with .json extension.
It is primarily used for exchanging data between server and web applications. It represents two strutured types like objects and arrays. Here an object is an 'unordered' collection of key value pairs and array is a collection of ordered values. Array may contain strings , booleans , numbers , null and may contain another array or objects also.
PREREQUISITES
-
Text Editor - STS(Spring Tool Suite was used for the examples)
-
Operating System - Linux (Ubuntu 14.04)
-
As there is no native support for JSON in JAVA for running the programs first we need to attach json-simple-1.1 jar into our project. We can download this jar file from - http://www.java2s.com/Code/Jar/j/Downloadjsonsimple11jar.htm
Lets have a look on JSON data representation.
Representation of JSON obect
{ "name": "kiran", "roll_num:": "1124", "subject": "master in computer applications" }
Representation of JSON object inside object .
{ "name": "kiran", "roll_num:": "1124", "subject": { "subect1": "Java", "subject2": "C++", "subject3": "Numerical Technique", "subject4": "Software Testing" } }
Here in the above example 'subject' is an another object inside the object.
Representation of JSON Array
{ "name": "kiran", "roll_num:": "1124", "subject": [ { "name": "Java", "duration": "1 yr" }, { "name": "C++", "duration": "6 months" }, { "name": "Numerical Technique", "duration": "1 yr" }, { "name": "Software Testing", "duration": "1.5 yr" } ] }
Here in the above example 'subject' is an array.
After briefing JSON structure lets read and write JSON data using java.
Reading JSON Object
-----------------------------
person.json
{ "name": "kiran", "roll_num:": "1124", "subject": "master in computer applications" }
Json_Java.java
import java.io.FileReader; import java.io.IOException; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class Json_Java { public static void main(String[] ar) { try { // parsing .json file and creating JSON object JSONObject jobj = (JSONObject) new JSONParser().parse(new FileReader("../person.json")); System.out.println("NAME IS : " + jobj.get("name")); System.out.println("SUBJECT IS : " + jobj.get("subject")); } catch (ParseException pe) { // parse exception System.out.println(pe); } catch (IOException ie) { // io exception System.out.println(ie); } } }
Output:
NAME IS : kiran SUBJECT IS : master in computer applications
Reading JSON Object inside Object
---------------------------------------------
person.json
{ "name": "kiran", "roll_num:": "1124", "subject": { "subect1": "Java", "subject2": "C++", "subject3": "Numerical Technique", "subject4": "Software Testing" }
Json_Java.java
System.out.println("SUBJECT IS : " + jobj.get("subject"));
Output:
SUBJECT IS : {"subect1":"Java","subject2":"C++","subject3":"Numerical Technique","subject4":"Software Testing"}
Reading JSON array
------------------------------
person.json
{ "name": "kiran", "roll_num:": "1124", "subject": [ { "name": "Java", "duration": "1 yr" }, { "name": "C++", "duration": "6 months" }, { "name": "Numerical Technique", "duration": "1 yr" }, { "name": "Software Testing", "duration": "1.5 yr" } ] }
Json_Java.java
JSONObject jobj = (JSONObject) new JSONParser().parse(new FileReader("../person.json")); JSONArray subjects = (JSONArray) jobj.get("subject"); System.out.println("SUBJECT IS : " + subjects.get(0));
Output:
SUBJECT IS : {"duration":"1 yr","name":"Java"}
Creating JSON Object
--------------------------------
Json_Java.java
JSONObject jobj = new JSONObject(); jobj.put("firstName", "Kiran"); jobj.put("lastName", "Sharma"); System.out.println(jobj);
Output:
{"firstName":"Kiran","lastName":"Sharma"}
Creating JSON array inside object
---------------------------------------------------
Example 1
Json_Java.java
JSONObject jobj = new JSONObject(); jobj.put("firstName", "Kiran"); jobj.put("lastName", "Sharma"); JSONArray jsonArray = new JSONArray(); jsonArray.add("subject1"); jsonArray.add("subject2"); jobj.put("subjects", jsonArray); System.out.println(jobj);
Output:
{"firstName":"Kiran","lastName":"Sharma","subjects":["subject1","subject2"]}
Example 2
Another example of creating JSON array
Json_Java.java
JSONObject jobj = new JSONObject(); jobj.put("firstName", "Kiran"); jobj.put("lastName", "Sharma"); JSONArray jsonArray = new JSONArray(); HashMap hm1 = new HashMap(); hm1.put("name", "Java Programming"); hm1.put("duration", "1 yr"); //adding object to json array jsonArray.add(hm1); jobj.put("subjects", jsonArray); hm1 = new HashMap(); hm1.put("name", "C++"); hm1.put("duration", "6 months"); jsonArray.add(hm1); hm1 = new HashMap(); hm1.put("name", "Software Testing"); hm1.put("duration", "1.5 yrs"); jsonArray.add(hm1); System.out.println(jobj);
Output:
{"firstName":"Kiran","lastName":"Sharma","subjects":[{"duration":"1 yr","name":"Java Programming"},{"duration":"6 months","name":"C++"},{"duration":"1.5 yrs","name":"Software Testing"}]}
Conclusion
After reading my blog you must have received the knowlege of how to parse JSON object and JSON array into java object. For learning further concepts about JSON data parsing we have further online links available. Here i am sharing the link in which you can learn how to convert json string to java object.
http://www.java67.com/2016/10/3-ways-to-convert-string-to-json-object-in-java.html
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Kiran Sharma
Kiran has good knowledge of java with Servlets, JSPs, Spring, and hibernate frameworks. She is very honest towards her work. Her hobby is listening to music.