JavaScript
  Home arrow JavaScript arrow Page 3 - JSON Basics
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JSON Basics
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2007-02-28

    Table of Contents:
  • JSON Basics
  • JSON basics
  • Array
  • JSON and XML
  • Root element with students and their attributes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

    More JavaScript Articles
    More By Jayaram Krishnaswamy


       · JSON has elements (data types, objects, arrays) similar to what one finds in...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek