JavaScript JSON Tutorial

Json is yet another way to parse data files, if you find parsing data with XML little complicated then JSON is your answer, it does not require any additional JavaScript Library and is the part of javaScript language. 

Json is all about Objects and dot notation theory. Which means...
 
myObject.Property
 
So how do we decalre objects in JavaScript?
 
var myObject = {};
 
Simply, now myObject is a empty object in javaScript. Now lets assign some properties to it
  
var myObject = {
 
                                    "Name":"Saqib"
                        };
var myName = myObject.Name // returns "Saqib"
 
see how simple was it?
 
so Lets get things little more complicated
 
var Myname = {
 
            "Profile":
 
            [
                        {"name":"Saqib","Age":25},
                        {"name":"John","Age":20},
            ]
}
 
Above we have a object called Myname which has an Array Profile with two nested Objects. so to get John's age syntax would be.
 
Myname.Profile[1].Age // Returns 20
 
Confused? dont be... lets try to understand how it works.
 
{} = Object in JavaScript
[] = Array in JavaScript
[1] = index of an array (which element you want to pull)
 
So in above example as you can see myName is a object (it has a {} ), Then it contains an array called Profile (it has a [] ), now within Profile we have two more objects (Both start with {}).
 
Below is a Simple working sample of a Json Example.
 
 
 
 
 
 
Please remember, if you are getting JSON code from AJAX/Web Service, then use eval() method to convert string into a Object.
 
json = eval(xml.responseText)
Posted by:

No comments posted.

Name :  

Email :  

Comment Below