Home arrow JavaScript arrow Page 2 - JSON Basics
JAVASCRIPT

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

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

Next: Array >>

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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