Wednesday, November 11, 2009

Overview of JSON with sample code



JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
These are universal data structures. Virtually all modern programming languages support them in one form or another.


The Object: An Introduction


Behold, an Object…
var myFirstObject = {};
It may not look like much, but those squiggly braces have the potential to record every bit of information humanity has ever gathered, and express the most complex programs computer scientists can dream up. In fact, Javascript itself is stored inside a set of squiggly braces just like that, as are all of its primitive data types -- strings, numbers, arrays, dates, regular expressions, they're all objects and they all started out just like myFirstObject.


Creating A New Object


The old way to create a new object was to use the new keyword.
var myJSON = new Object();
This method has been deprecated now in favor of simply defining an empty object with squigly braces…
var myJSON = {};


Objects as Data


At its most base level a Javascript Object is a very flexible and robust data format expressed as name/value pairs. That is, an object holds a name which is an object's property -- think of it as a plain old variable name that's attached to the object name. And the object holds the value of that name. Here's an example…
var myFirstJSON = { "firstName" : "John",
                   
"lastName"  : "Doe",
                   
"age"       : 23 };

document
.writeln(myFirstJSON.firstName);  // Outputs John
document
.writeln(myFirstJSON.lastName);   // Outputs Doe
document
.writeln(myFirstJSON.age);        // Outputs 23
This object has 3 properties or name/value pairs. The name is a string -- in our example, firstName, lastName, and age. The value can be any Javascript object (and remember everything in Javascript is an object so the value can be a string, number, array, function, even other Objects) -- In this example our values are John, Doe, and 23. John and Doe are strings but age is a number and as you can see this is not a problem.
This data format is called JSON for JavaScript Object Notation. What makes it particularly powerful is that since the value can be any data type, you can store other arrays and other objects, nesting them as deeply as you need. 


Accessing Data In JSON


The most common way to access JSON data is through dot notation. This is simply the object name followed by a period and then followed by the name/property you would like to access.


var myObject = { 'color' : 'blue' };



document
.writeln(myObject.color); // outputs blue.
If your object contains an object then just add another period and name…


var myObject = { 'color' : 'blue',

                 
'animal' : {'dog' : 'friendly' }

               
};



document
.writeln(myObject.animal.dog); // outputs friendly


Stumble
Delicious
Technorati
Twitter

0 Comments:

Post a Comment