Home arrow JavaScript arrow Page 5 - JSON Basics
JAVASCRIPT

JSON Basics


These are the golden days of JavaScript, which was warily used in the not too distant past because of the browser wars (still being waged). With enhancements to JavaScript in recent years and the advent of AJAX, interest in Javascript has taken a new turn, a turn for the better. Early on with AJAX it was recognized that there was a contender for XML for handling data which was stable, faster, and portable. This was the beginning of JSON.

Author Info:
By: Jayaram Krishnaswamy
Rating: 4 stars4 stars4 stars4 stars4 stars / 14
February 28, 2007
TABLE OF CONTENTS:
  1. · JSON Basics
  2. · JSON basics
  3. · Array
  4. · JSON and XML
  5. · Root element with students and their attributes

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JSON Basics - Root element with students and their attributes
(Page 5 of 5 )

The root element is the object with the three students arranged in an array. Each of the students is an object as shown in the next listing. This is an object whose value is an array. The wclass[1] refers to the second student object and is accessed as shown in the alert statement. This code will display an alert window with value 2.

<SCRIPT LANGUAGE="JavaScript">
var justStudents={"wclass":[
{"student":{"id":"1"}},
{"student":{"id":"2"}},
{"student":{"id":"3"}}
]};
alert(justStudents.wclass[1].student.id); 
</SCRIPT>

In the above code the children of the students are not included.

Root element with student's children

The next listing shows the JSON object with only the children of students shown in an array. Here student 2 is "Charles Boyer" and his legacyskill is displayed in the alert box.

<SCRIPT LANGUAGE="JavaScript">
var webclass ={
"wclass":[
{"student":
{"name":"Linda Jones", "legacySkill":"Access, VB 5.0"}},
{"student":
{"name":"Adam Davidson", "legacySkill":"Cobol, MainFrame"}},
{"student":
{"name":"Charles Boyer", "legacySkill":"HTML, XML"}}
]
};
alert(webclass.wclass[1].student.legacySkill);
</SCRIPT>

Combining the two, it is easy to see how the JSON equivalent of webstudents.xml can be fashioned as shown in the next listing. The listing also shows how the various values are accessed.

 <SCRIPT LANGUAGE="JavaScript">
var studentAll={"wclass":[
{"student":{"id":"1"},"name":"Linda Jones", 
"legacySkill":"Access, VB 5.0"}, {"student":{"id":"2"},"name":"Adam Davidson",
"legacySkill":"Cobol, MainFrame"}, {"student":{"id":"3"},"name":"Charles Boyer",
"legacySkill":"HTML, XML"} ]}; document.write("<b>Id of 3rd student: </b>" + studentAll.wclass[2].
student.id); document.write("<br/>"); document.write("<b>name of 2nd student:</b> " + studentAll.wclass[1].
name); document.write("<br/>"); document.write("<b>legacySkill of 1st student:</b> " + studentAll.
wclass[0].legacySkill); </SCRIPT>

The displayed output when this script is executed is as shown in the next picture. Notice how the student attributes are accessed.

Summary

The tutorial has introduced JSON, JavaScript Object Notation (RFC 4627) with examples of its types, objects and structures. JSON is well suited for data interchange. JSON is stable because it has no versions; needs no validation; and is not extensible, all very likable characteristics which may make it a long time player in JavaScript. However, since JSON is a subset of JavaScript, it has to conform to the same naming conventions regarding the use of language specific keywords and other rules.

Also, its usefulness in AJAX calls is obvious because JSON is as easy for humans as it is for machines, and a lot simpler and more understandable. In the simple example of webstudents.xml, the number of characters for XML amounted to 413, whereas when formatted in JSON it required 243, a substantial difference. Accessing values is also a lot simpler than going through the ECMA object model climbing up and down the XML tree. Future articles will deal with other features of JSON.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials