JSON – JavaScript Object Notation
November 8, 2010 Leave a Comment
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. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999. 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.
These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangable with programming languages also be based on these structures.
JSONArray is a sequential and ordered way of collection values.It may consists of Boolean, JSONArray, JSONObject, Number and String or the JSONObject.NULL objects.
To have functionality of JSON in your java program you must have JSON-lib. JSON-lib also requires following “JAR” files:
1. commons-lang.jar
2. commons-beanutils.jar
3. commons-collections.jar
4. commons-logging.jar
5. ezmorph.jar
6. json-lib-2.2.2-jdk15.jar
JSON-lib is a java library for that transforms beans, collections, maps, java arrays and XML to JSON and then for retransforming them back to beans, collections, maps and others.
In this example we are going to use JSONArray for creating an object of JSONArray and then we will print this array object . For using JSONArray class we have to import package “net.sf.json”. To add elements in this object we have used add() method. Here is the full example code of JSONJavaArray.java as follows:
JSON simple
http://code.google.com/p/json-simple/downloads/detail?name=json_simple-1.0.2.jar&can=2&q=
import org.json.simple.JSONObject;
public class JSONExample {
public static void main(String[] args) {
System.out.println();
JSONObject js = new JSONObject();
js.put(“1″,”first”);
js.put(“2″,”second”);
System.out.println(js);
}
}
output is – {“2″:”second”,”1″:”first”}
Example 2
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JSONExample {
public static void main(String[] args) {
System.out.println();
JSONObject js = new JSONObject();
js.put(“1″,”first”);
js.put(“2″,”second”);
JSONArray ar = new JSONArray();
ar.add(“1″);
ar.add(“2″);
System.out.println(ar);
}
}
output is — ["1","2"]
Example 3
import net.sf.json.JSONArray;
public class JSONJavaArray
{
public static void main(String args[]){
JSONArray arrayObj=new JSONArray();
arrayObj.add(“string”);
arrayObj.add(new Integer(100));
arrayObj.add(new Double(40));
System.out.println(arrayObj);
JSONObject array= new JSONObject();
array.put("data1","Hello");
array.put("data2","World");
System.out.println(array);
}
}