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 
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 - 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

    - 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek