Datasheet

Chapter 1: AJAX Technologies
20
var firstNameElement = employeeInfo.childNodes[0];
var firstname = firstNameElement.firstChild.nodeValue;
var lastNameElement = employeeInfo.childNodes[1];
var lastname = lastNameElement.firstChild.nodeValue;
var employeeIdElement = employeeInfo.childNodes[2];
var employeeid = employeeIdElement.firstChild.nodeValue;
var departmentNameElement = employeeInfo.childNodes[3];
var departmentname = departmentNameElement.firstChild.nodeValue;
Finally, it instantiates and returns an employee object with the returned firstname , lastname ,
employeeid , and departmentname :
return new employee(firstname, lastname, employeeid, departmentname);
JSON
One of the main tasks in an AJAX-enabled application is to serialize client/server-side objects into the
appropriate format before data is sent over the wire and to deserialize client/server-side objects
from an appropriate format after data is received over the wire. In general there are two common
data- interchange formats: XML and JSON. XML format was discussed in the previous section. Now let’s
move on to the second common data-interchange format: JSON.
JavaScript Object Notation ( JSON ) is a data-interchange format based on a subset of the JavaScript lan-
guage. The following sections present the fundamental JSON concepts and terms.
object
A JSON object is an unordered, comma-separated list of name/value pairs enclosed within a pair of
braces. The name and value parts of each name/value pair are separated by a colon (
: ). The name part
of each name/value pair is a string; and the value part is an array, another object, a string, a number,
true , false , or null .
array
A JSON array is an ordered, comma-separated list of values enclosed within a pair of square brackets
(
[ ] ). Each value is an array, another object, a string, a number, true , false , or null .
string
A JSON string is a collection of zero or more Unicode characters enclosed within double quotes ( “ “ ).
You must use a JSON string to represent a single character, and the character must be in double
quotes. You must use the backslash character (
\ ) to escape the following characters:
❑ Quotation mark ( \" )
❑ Solidus ( \/ )
c01.indd 20c01.indd 20 8/20/07 5:40:11 PM8/20/07 5:40:11 PM