Pages

Monday, May 18, 2009

Java Script Object Notation

Share it Please

Java Script Object Notation

Json is nothing but java script object notation.this is a universal , language independent format for data interchange.it is similar to xml but xml uses tags where are json uses object literal notation of java script.so this is much simpler than xml . json is a data structuring format which is extremely light weight and simple.even though json is native to java script it can be handled by many other languages when java script plays the role of data exchange in cases ajax sort of applications.

It is very usefull to use json whenever java script playes a role in sending data or at the receiving end.because when we use java script with xml we need to write special scripts or should interact with DOM . but when we use json which is simple object notation and doesnot require much overhead in handling it.there are many libraries in many other languages for handling the json objects.

Json Notation :

Generally in java script arrays are implemented in ,

var myFruits = ["apple","orange","banana"]; , and u can access the myFruits[0] .

similarly in json u can implement the above thing in

var fruitColors = { "apple":"green", "banana":"yellow", "orange":"orange" };

in json u can simpley specify object : value pairs. In the above case we have a fruitColors which has fruitName:colorName .You can access the json data similarly like accessing the array in java script .

alert(fruitColors['apple']); // Alerts "green"

alert(fruitColors['banana']); // Alerts "yellow"

alert(fruitColors['orange']); // Alerts "orange"

Here is simple single dimension element

{

"book-title": "head first java",

"pages": 500,

"id": 4536

}

Another single dimension element is

[

"banana",

"apple",

"orange"

]

You can use both ways to define a json object

You can use both ways to define a json object.but normally it would better if we use both in a single way .

[

{

"title": "Pro JavaScript Techniques",

"pages": 384,

"isbn": "978-1590597279"

},

{

"title": "Professional Ajax",

"pages": 624,

"isbn": "978-0470109496"

}

]

I defined a json object with 2 book objects . I can access the values by

alert(objectName[0]['title']); // Alerts "Pro Javascript Techniques" alert(objectName[1]['title']); // Alerts "Professional Ajax" alert(objectName[1]['isbn']); // Alerts "978-0470109496"

GWT support to Ajax: gwt gives us a way to handle json data . it has various classes like JSONString , JSONObject and many other classes handle the json data.classes releated to the handling of Json data in gwt are available in package com.google.gwt.json.client.

NOTE : Json Objects in Gwt cannot be serialized . so it cannot be moved through wire.we can just get a String representaion of the Json value and send it through wire.

No comments :

Post a Comment