Mastering JSON ( JavaScript Object Notation )
Widely hailed as the successor to XML in the browser, JSON aspires to be nothing more than a simple, and elegant data format for the exchange of information between the browser and server; and in doing this simple task it will usher in the next version of the World Wide Web itself.
The Object: An Introduction
Behold, an Object…
varmyFirstObject ={};
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.
varmyJSON=newObject();
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. Here is an example of a somewhat complex JSON structure…
var employees = { "accounting" : [ // accounting is an array in employees.
{ "firstName" : "John", // First element
"lastName" : "Doe",
"age" : 23 },
{ "firstName" : "Mary", // Second Element
"lastName" : "Smith",
"age" : 32 }
], // End "accounting" array.
"sales" : [ // Sales is another array in employees.
{ "firstName" : "Sally", // First Element
"lastName" : "Green",
"age" : 27 },
{ "firstName" : "Jim", // Second Element
"lastName" : "Galley",
"age" : 41 }
] // End "sales" Array.
} // End Employees
Here employees is an object. That object has two properties or name/value pairs. Accounting is an array which holds two JSON objects showing the names and age of 2 employees. Likewise sales is also an array which holds two JSON objects showing the name and ago of the two employees who work in sales. All of this data exists within the employees object. There are several different ways to access this data.
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
Using the "employee" example above, if we wanted to access the first person who worked in sales…
document.writeln(employees.sales[0].firstName + ' ' + employees.sales[0].lastName);
We can also access the second person who works in "accounting".
document.writeln(employees.accounting[1].firstName + ' ' + employees.accounting[1].lastName);
To recap, the "employee" example is an object which holds two arrays each of which holds two additional objects. The only limits to the structure are the amount of storage and memory available to it. Because JSON can store objects within objects within objects and arrays within arrays that can also store objects, there is no virtual limit to what a JSON object can store. Given enough memory and storage requirement, a simple JSON data structure can store, and properly index, all the information ever generated by humanity.
Simulating An Associative Array
You can also access JSON data as if it were an Associative Array.
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
Be aware that this is NOT an associative array, however it appears. If you attempt to loop through myFirstObject you will get, in addition to the three properties above, any methods or prototypes assigned to the object, so while you're more than free to use this method of addressing JSON data, just treat it for what it is (Object Properties) and not for what it is not (Associative Array).
Mais em: http://www.hunlock.com/blogs/Mastering_JSON_(_JavaScript_Object_Notation_)
Sem comentários:
Enviar um comentário