JavaScript
  Home arrow JavaScript arrow Page 2 - 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 - JSON basics


    (Page 2 of 5 )

    JSON has simple types and two structures which are very similar to the universally used data structures such as dictionary objects, hash values, key/value pairs, lists, sequences, record sets, arrays and so on. It is a natural.

    Types in JSON

    JSON's JavaScript subset admits of the following four primitive types.

    • strings [except ", and Control Characters]
    • number [Integer, real and Floating point]
    • Boolean [literals, true and false]
    • null

    Unlike JavaScript it does not support hexadecimal and octal representation of numbers, nor does it support NaN (not a number) and infinity. Also numbers do not need quotes but strings do.

    In addition to the four primitives it also supports two kinds of structures, object and array.

    JSON Object

    A JSON object is an unordered set (members) of key/value pairs, with keys being separated by values using a colon (:) and the members being separated by a comma (,). The colon and the comma are respectively called the name separator and the value separator.

    Here is a variable that represents a JSON object with three members. "company":"Hodentek" is one member of the JSON object where the key, which is "company," is separated by the value, which is "Hodentek." The object is always enclosed between curly brackets as shown in the declared variable. In this example all the values are of type string.

    var jObj={"company":"Hodentek", "phone":"609-275-1205", "city":
    "Plainsboro"};

    You can access any member's value using the dot notation as shown in the next snippet, which shows the "phone" of the object in the alert as "609-275-1205."

    alert(jObj.phone);//
    

    The values of members of the JSON object can be of different types as shown in the following code listing.

    <SCRIPT LANGUAGE="JavaScript">
    //values are numbers (integer and floating point)
    var jobj2={"sunday":1, "tuesday":2.85, "wednesday":3};
    document.write("<b>jobj2.tuesday:</b> " + jobj2.tuesday +"<br>");
    //values are of mixed types
    var jObj3={"culprit":true, "innocent":false, "notsure":null};
    document.write("<b>jObj3.culprit:</b> " + jObj3.culprit +"<br>");
    document.write("<b>jObj3.innocent:</b> " + jObj3.innocent +"<br>");
    document.write("<b>jObj3.notsure:</b> " + jObj3.notsure +"<br>");
    </SCRIPT>
    

    The displayed output when this code is browsed is as shown in the next picture.

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