JavaScript
  Home arrow JavaScript arrow Page 5 - 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 
Sun Developer Network 
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: 5 stars5 stars5 stars5 stars5 stars / 5
    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 - 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.

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

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT