JSON is mainly based on two structures:
A collection of name/value pairs which can be thought of as
an object, structure, hash table, keyed list, and dictionary in most of the
Object Oriented Languages.
A list of values which is similar to the array in most
Object Oriented Programming Languages.
JSON Object
An object is represented as follows:
Listing 2: JSON Object
{
// Properties (name/value pairs separated by comma)
}
When you want to construct a JSON object you simply start by
opening right opening curly braces then write down your name/value pairs which
are thought of as properties in most of OOP languages.
The object can contain simple name/value pairs, an array of
values, an array of objects, and simple values.
Once you have finished writing your own properties, you can
add left opening curly braces as if you are closing the object. For those of
you who have an experience with C# or JAVA, in those two languages we use the
left and right opening curly braces to specify the beginning and ending of an
object.
To access a property within your JSON object in JavaScript,
you simply write the following:
Listing 3: JSON Object Access
var fname = jsonObject.FirstName
To be able to access a JSON object this way as shown in Listing
3 above, you will have to first parse the string JSON object generated by a
server-side language into a JSON object. You can simply use the eval function
in JavaScript; however, this function has many security concerns because it can
execute any JavaScript statement. A better and clearer way of parsing your
string JSON objects to real JSON objects is to use a library that is
distributed for free from www.json.org. I
will look at this library later on in this article.
JSON Array
A JSON array is a simple array structure that I am sure you
have extensively used in your applications while developing either windows or
web applications.
In JSON an array can hold simple values or objects. An
example of a JSON array is as follows:
Listing 4: JSON Simple Array
"simpleArray": ["value 1", "value 2", "value 3"]
As you can see, I have constructed a simple array of strings
named simplearray. You can even construct an array of integers, floats, double,
and even Boolean values. The reason behind naming the array is to be able
access its elements. This will be clearer in the next listing where I show you
how to access a complex array of objects.
You can also have a more complicated array where you might
embed an array of objects.
Listing 5: JSON Complex
"arrayOfObjects":
[
{
"name1": "value1",
"name2": "value2"
},
{
"name3": "value3",
"name4": "value4"
}
]
As shown in the above listing, each element is simply a JSON
object which is normally allowed in JSON. To access the first property of the
second object in the above array, you might write something similar to this
code:
Listing 6: JSON Complex Array Access
var name1 = arrayOfObjects[1].name1
Again, if you are going to use the above code you will have
to parse the JSON string array into a real JSON array. More on this comes in
the next sections of this article.
JSON Property
As mentioned above, JSON is based upon the object and array
data structures. Within these data structures you would definitely use
properties and values.
A value can be a string, number, object, array, true, false,
or null values.
A property is nothing but a value with a name. You can think
of each name/value pair as a property.
To have an extensive explanation on the JSON data types
please refer to the official JSON website which can be reached at the following
URL.