Home arrow JavaScript arrow Page 3 - 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 - Array
(Page 3 of 5 )

Unlike JSON objects, JSON arrays are enclosed between square braces [ ]. The JSON array is an ordered sequence of values separated by a comma (,). The values can be any of the primitive types as well as the two structures, JSON objects and JSON arrays. Here is a JSON array variable with four string values.

var myJsonArray=["Red", "Blue", "Yellow", "Magenta"];

You will be able to access an array item from its position (0 based) as shown in the following listing.

<SCRIPT LANGUAGE="JavaScript">
var myJsonArray=["Red", "Blue", "Yellow", "Magenta"];
for (i=0; i<4; i++){
document.write("<b>myJsonArray["+i+"]</b>="+ myJsonArray[i]+"<br>");
}
</SCRIPT>

The displayed output when this script is executed is as shown in the next picture.

It is possible to have an array  whose elements are of different types. Array items can be objects and arrays as well, as shown in the next listing.

<SCRIPT LANGUAGE="JavaScript">
//Array with different types
myJA2=["red", 1, null, true];
document.write("<b>myJA2:</b>  " + myJA2[2]);
document.write("<p></p>");
//Nested array
myJA3=["red",1,["a", "b", "c"], false];
document.write("<b>myJA3[2][1]:</b>  " + myJA3[2][1]);
document.write("<p></p>");
//Object in an array
myJA4=["red", {"Yes":"Yes, you do", "No":"No, you do not"}, 2];
document.write("<b>myJA4[1].Yes: </b>"+ myJA4[1].Yes);
</SCRIPT>

The displayed output when this script is executed is as shown in the next picture. Notice how the inner array and the object inside the array are  accessed.


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