Monday, 27 June 2011

JSON - JavaScript Object Notation in JavaScript


JSON stands for JavaScript Object Notation that is a language independent text format which is fast and easy to understand.
In another words we can say that JavaScript Object Notation is a lightweight data-interchange format that is completely language independent but with some conventions.
So in this section of JSON tutorial we are going to discuss JSON with JavaScript. 

Creating Object in JavaScript using JSON
We can create objects in JSON with JavaScript in many ways :
1. "var JSONObjectName ={};" will create an empty object. 
2. "var JSONObjectName= new Object();" will create a new Object.
3. "var JSONObjectName = {"name ":"Ravi", "age":23}; will create an Object with attribute name which contains value in String and age with numeric value.
Now by creating this object we can access attributes by only "." operator.
Here is the full example code for creating an Object in JavaScript using JSON:

ObjectJavaScript-JSON.htm
<html>
<head>
<title>
    Object creation in JSON in JavaScript
</title>

<script language="javascript">
var JSONObject = {"name" : "Ravi",
  "address"  : "New Delhi",
  "age"  : 27,
   "phone"   : "011-1234567",
   "MobileNo"  : 01234567899
  };
document.write("<h2><font color='blue'>Name</font>::"
   +JSONObject.name+"</h2>"); 
document.write("<h2><font color='blue'>Address</font>::"
   +JSONObject.address+"</h2>"); 
document.write("<h2><font color='blue'>Age</font>::"
   +JSONObject.age+"</h2>"); 
document.write("<h2><font color='blue'>Phone No.</font>::"
   +JSONObject.phone+"</h2>"); 
document.write("<h2><font color='blue'>Mobile No.</font>::"
   +JSONObject.MobileNo+"</h2>"); 
</script>

</head>
<body>
          <h3>Example of object creation in JSON in JavaScript</h3>
</body>
</html>


Output:

Name::Ravi

Address::New Delhi

Age::27

Phone No.::011-1234567

Mobile No.::1234567899

Example of object creation in JSON in JavaScript

2 comments:

  1. This is to good, thanks for sharing your knowledge with us. Following links also helped me and develpers.

    http://msdn.microsoft.com/en-us/library/bb299886.aspx

    http://www.mindstick.com/Articles/af150297-b1e0-4316-958e-5a0b11a4a016/?JavaScript%20Object%20Notation%20JSON%20in%20JavaScript%20and%20Net

    ReplyDelete
  2. Well explanation sir, you have done a good job

    ReplyDelete