Configure HTML/JavaScript

Showing posts with label json serialization. Show all posts
Showing posts with label json serialization. Show all posts

Saturday, March 30, 2013

json serialization

What is JSON

Json stands for JavaScript Object Notification. it represents data in object format so that javascript and understand it easily and manipulate it easily

it looks like as below:
[{"id":"83","text":"sales crescent"},{"id":"88","text":"sales grey bar"}]
 its just a simple array of two json object

keep in mind that, if you will find [] symbol it means it's a array of object. that means one or more then one json object may be there inside the space

like bellow
var mathTen = 
[{
"name": "Aniruddha Das",
"email": "someemailaddress@gmail.com
"valuepaids": [{"id":"83","text":"sales crescent"},{"id":"88","text":"sales grey bar"}]
}]

you can also iterate this object by using jQuery.each() ot any normal javascript loop.

here variable mathTen contain a array of json. again inside mathTen there valuepaids node which contains a array of json object.

there are ways to manipulate these json object hierarchy inside in side javascript code (i am not mentioning here as we can do that , i think so :) )

Where we need json serialization:

We need json serialization where we want to pass json from one place to another. like from one environment to another. like while sending json data from java/php/asp.net codes to javascript code thorugh ajax call.

Or sending json data as parameter from a dynamically created javascript function

But in some situations we need to create dynamic html codes and pass as json as as parameters if we are creating dynamic javascript functions in that like below
var yuyu = [{"id":"83","text":"sales crescent"},{"id":"88","text":"sales grey bar"}];
indObj=[{"id":"83","text":"sales crescent"}];


$("#divAllIndividuals")
            .append("+indObj[0].id+"','"+JSON.stringify(yuyu)+"','"+JSON.stringify(indObj)+"','"+indObj[0].text+"') title=\"Delete Entry\">\"");

there json variable (yuyu and indObj) will no more will be json in the called function if you will not serialize these function here. here the serialize concept come


what is json serialization:

Json serialization is nothing but it convert json object to string so that you can easily send that string from one environment to another or you can pass that string as parameter from a dynamically created javascript function.

Use of json serialization:

to serialize a json object we can use JSON.stringify() as we did in the above created dynamically created javascript function. this function will convert json object to string.

Again we have to deserialize the json object string in the called function  (if we are calling in as parameter in dynamic javascript function) or if we are receiving it from other language like java/php/asp.net.

to deserilize json object string we need to call JSON.parse(); it will again convert josn object string to json object so that we can use json nodes in your program.

var indObj = $.parseJSON(jsonpassedvariablehere);


that it. now you can send json data from anywhere in your program and can use that anywhere in your program by serialize and deserialize the objects

Thanks