JSON Example


JSON is a subset of javascript. Its a lightweight for data representations. Lets see the example.

JSON response should be like this.

bookobj=
[
“book1”:{
“bookname”:”ajax”,
“price”:600,
“chapters”:{”chapter1″:”Ajax Basics”, “chapter2″:”Ajax Examples”, “chapter3″:”Using JSON in Ajax”},
“authors”:[“orielly”,”nicholas”]
},
“book2”:{
“bookname”:”json”,
“price”:800,
“chapters”:{”chapter1″:”JSON Basics”, “chapter2″:”JSON DataTypes”, “chapter3″:”JSON Examples”},
“authors”:[“orielly”,”douglas crockford”]
}
]

Code to retrieve the data from the above response.

bookobj.book1.bookname – return the value of bookname “ajax”
bookobj.book1.chapters.chapter1 – return value of chapter1 in book1 “Ajax Basics”
bookobj.book2.authors[1] – return the value of book2 author “douglas crockford”
bookobj.book2.price – this will return the price of book2. “800”

Example for execute the scripts in the JSON data.
<html>
<body>
<div id=”titleid” style=”display:none;”>Book Name</div>
<div id=”bookname”></div>
</body>
<script>
function showTitle()
{
document.getElementById(‘titleid’).style.display = ”;
}
function showData(param)
{
document.getElementById(‘bookname’).innerHTML = param;
}
</script>
</html>

JSON response.

obj=
{
“callmethod1″:”showTitle()”,
“callmethod2″:”showData(‘Ajax’)”
}

execute the scripts using javascript eval function.

eval(obj.callmethod1); – this will execute the javascript showTitle method.
eval(obj.callmethod2); – this will execute the javascript showData method.

Are you new to JSON, pls read this first https://manikandanmv.wordpress.com/2008/10/08/json-java-script-object-notation/

Advertisements

Tags: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: