Home arrow JavaScript arrow Page 4 - 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 - JSON and XML
(Page 4 of 5 )

JSON is minimal textual representation of data as compared to the "verbose" XML, which makes equivalent XML much larger than JSON. Well, both are textual, but JSON is more so than XML with all its "angularities." This is not to put down XML but just to highlight the obvious. This has obvious implications for AJAX, and more often JSON is the preferred data format in which AJAX results are received. Everything in XML is a string but JSON has types as discussed earlier.

JSON equivalent of XML

In my previous tutorials, XML Islands and XML Responses and AJAX, the file webstudents.xml was used for illustration. The same file will be represented as a JSON object in this section. The file is reproduced here for reference. In writing down the equivalence only the root element and its children are considered. Here is the listing of webstudents.xml.

<wclass>
<!--My students who took web programming class with me-->
<student id="1">
<name>Linda Jones</name> 
<legacySkill>Access, VB5.0</legacySkill>
</student>
<student id="2">
<name>Adam Davidson</name> 
<legacySkill>Cobol, MainFrame</legacySkill>
</student>
<student id="3">
<name>Charles Boyer</name> 
<legacySkill>HTML, Photoshop</legacySkill>
</student>
<student id="4">
<name>Charles Mann</name> 
<legacySkill>Cobol, MainFrame</legacySkill>
</student>
</wclass> 

The rules in this conversion are simple. The root element will be the main object, and the three student nodes will form an array. Each array element will be a student object with its own attributes as a string value pair. If you keep the following syntax  in your cross-hairs it will be easy to follow the listing.

Object:
var obj={key1:value1, key2:value2 ,key3:value3};
Array:
var Arr=[valueA, valueB, valueC];

The XML document has both simple and complex types and this is developed piecewise to make the explanation easy to understand.


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 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials