AJAX Basic Part – III

Hi,

today we will do next  topic so lets begin

5. What is JSON ?
Ans: JSON is JavaScript Object Notation. It’s a light weight Data interchange format which is independent from language. Its easy to read & Write by human & easy to parse & genrate by language. The attributes is seprated with “,” (comma) and value of attributes are define after colon symbol(“:”) .
Ex:- {Data:[{“fname” : “RAJAT”, “Lname” : “JAISWAL” }]}
The above is JSON example in which there are two attributes fname, Lname and the are seprated by “,” (comma) symbol.
And there value is “RAJAT” & JAISWAL” which are define after “:” (colon) symbol.
I am attaching  project file just go through it.

One of the most important work here is to parse json

We do response with following format

 

 

 

 {“Bank”:[{“lngId” : “1”, “strCode” :”ICICI” ,”strBank” : “ICICI BANK” },{“lngId” : “2”, “strCode” :”HDFC” ,”strBank” : “HDFC BANK” }]}

 When we parse with json then here is the code for it.

  function myServerResponse() {
            if (myServerRequest.readyState == 4) {
                if (myServerRequest.status == 200) {
                    var tdata;
                    tdata = eval(‘(‘+ myServerRequest.responseText +’)’);
                    var intI;
                    intI = 0;
                    var strResult =”<table border=’1′ cellspacing=’4′ cellpadding=’5′><TR><TD>lngId</td><td>code</td><td>Bank</td></tr>”
                    for (intI = 0; intI <= tdata.bank.length – 1; intI++) {
                      strResult = strResult + “<tr><td>” + tdata.bank[intI].lngId + “</td><td>” + tdata.bank[intI].strCode + “</td><td>” + tdata.bank[intI].strBank + “</td></tr>”;
                    }
                   
                    strResult = strResult + “</table>”
                    document.getElementById(‘dvResult’).innerHTML = strResult;
                } else {
                    alert(‘error’);
                }
            }
            return false;

        }

 Here we first eval the responseText then  we just use it as collection array as shown in above example.

tdata.bank[0].lngId = 1

tdata.bank[0].strCode =”ICICI”

tdata.bank[0].strBank =”ICICI BANK”

in the above manner we use the Json which is meaning ful and simple format.

for more information check out the attached project. bankmasterProject

Thanks

Rajat

Advertisements

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