HTML and CSS Reference
In-Depth Information
> person.address = {};
> = 'Los Angeles';
> person.address.postCode = 90245;
The example above consists of two objects. The first object is a person; this then contains
a second object that captures the address information.
It would not be difficult to create an object from the XML structure. There is however a
data format that is far more closely aligned with JavaScript called the JavaScript Object
Notation (JSON) that makes this process trivial.
You can transform this object into a JSON encoded string as follows:
> JSON.stringify(person)
"{"firstName":"John","lastName":"Smith","age":32,"address":{"city":"Los Angeles","postCode":90245}}"
The output of this function call is a string, and could be assigned to a variable:
> s = JSON.stringify(person)
A string can then be transformed back into an object as follows:
> person2 = JSON.parse(s)
The process of converting between a string and an object is called serializing and de-seri-
alizing: the string is a serialized form of the object.
This process is only applicable for properties: methods are not retained when an
object is serialized.
JSON is a remarkably simple data format. The entire data format is contained in a couple
of paragraphs at the following web site:
There are 3 types of value in JSON:
1. Objects , denoted with the now familiar curly brackets
Search WWH ::

Custom Search