Saturday, July 2, 2011

A brief on JSON Message and JSON Object - Java

After walking through the XML as a one of most important media of data exchange, I have encountered another media of data exchange which is more light weight and does not involve the burden of parsing like XML does have – the message format is called JSON (Java Script Object Notation).
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.
JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
JSON is built on two structures:
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
JSON Message Example:
{"Employee":
{
"Employee1":
{"firstname":"Snehanshu",
"lastname":"Chatterjee",
"company":"TM",
"grade":"TA",
"post":"G1"
},
"Employee2":
{"firstname":"Summet",
"lastname":"Chakraborty",
"company":"TM",
"grade":"STA",
"post":"G2"
}
}
}

XML equivalent:
          <?xml version="1.0" encoding="UTF-8" ?>    
         <Employee>
          <Employee1>
 <firstname>Snehanshu</firstname>
 <lastname>Chatterjee</lastname>
 <company>TM</company>
 <grade>TA</grade>
  <post>G1</post>
      </Employee1>
  <Employee2>
      <firstname>Summet</firstname>
<lastname>Chakraborty</lastname>
<company>TM</company>
      <grade>STA</grade>
     <post>G2</post> 
    </Employee2>
     </Employee>

The JSON data interchange format is easily supported in Java.
There are many jars available for using JSON in JAVA. The one which I have used is “jettison-1.3.jar” – available easily.
The APL is available also in here.
The class  which I shall be using in creating a JSON message is JSONObject.
A simple program to create above JSON Message
import org.codehaus.jettison.json.JSONObject;

public class JSONExample {
      public static void main(String[] args) {
            JSONObject employee1 = null;
            JSONObject employee2 = null;
            JSONObject employees = null;
           
            JSONObject employeeHeader = null;
            try {
                  employee1 = new JSONObject();
                 
                  employee1.put("firstname","Snehanshu");
                  employee1.put("lastname","Chatterjee");
                  employee1.put("company","TM");
                  employee1.put("grade","TA");
                  employee1.put("post","G1");
                 
                  employee2 = new JSONObject();
                 
                  employee2.put("firstname","Summet");
                  employee2.put("lastname","Chakraborty");
                  employee2.put("company","TM");
                  employee2.put("grade","STA");
                  employee2.put("post","G2");
                 
                  employees = new JSONObject();
                 
                  employees.put("Employee1", employee1);
                  employees.put("Employee2", employee2);
                 
                  employeeHeader = new JSONObject();
                 
                  employeeHeader.put("Employee", employees);
                 
                  System.out.println(employeeHeader);
                 
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
}
The output of the following program is
{"Employee":{"Employee1":{"firstname":"Snehanshu","lastname":"Chatterjee","company":"TM","grade":"TA","post":"G1"},"Employee2":{"firstname":"Summet","lastname":"Chakraborty","company":"TM","grade":"STA","post":"G2"}}}
Hope this might be useful for you!

No comments:

Total Pageviews