JSON is a very lightweight data format based on a subset of the JavaScript syntax, namely array and object literals. Because it uses JavaScript syntax, JSON definitions can be included within JavaScript files and accessed without the extra parsing that comes along with XML-based languages. But before you can use JSON, it's important to understand the specific JavaScript syntax for array and object literals.
For those unfamiliar with JavaScript literal notation, array literals are specified by using square brackets ([ and ]) to enclose a comma-delimited list of JavaScript values (meaning a string, number, Boolean, or null value), such as:
You can then access the values in the array by using the array name and bracket notation:
alert(aNames[0]); //outputs "Benjamin" alert(aNames[1]); //outputs "Michael" alert(aNames[2]); //outputs "Scott"
Note that the first position in the array is 0, and the value in that position is "Benjamin".
Because arrays in JavaScript are not typed, they can be used to store any number of different data types:
var aValues = ["string", 24, true, null];
This array contains a string, followed by a number, followed by a Boolean, followed by a null value. This is completely legal and perfectly fine JavaScript.
If you were to define an array without using literal notation, you would have to use the Array constructor, such as: