Home arrow JavaScript arrow Page 3 - JSON Basics

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 / 27
February 28, 2007
  1. · JSON Basics
  2. · JSON basics
  3. · Array
  4. · JSON and XML
  5. · Root element with students and their attributes

print this article

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.

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

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.

//Array with different types
myJA2=["red", 1, null, true];
document.write("<b>myJA2:</b>  " + myJA2[2]);
//Nested array
myJA3=["red",1,["a", "b", "c"], false];
document.write("<b>myJA3[2][1]:</b>  " + myJA3[2][1]);
//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);

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

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


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