05.01.2013 Views

hide - Understanding jQuery

hide - Understanding jQuery

hide - Understanding jQuery

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

More About JSON<br />

JSON is a data interchange format. Although it is influenced by the JavaScript syntax it is used<br />

in many other languages to represent data. It is not unique to JavaScript. In fact, it is often used<br />

to send already created objects over network to a program written in another language (such<br />

as Java) as string. Once Java receives that string it can then recreate a Java object using the<br />

JSON string that was received from an application written in another language.<br />

To create an object in JavaScript, we can use the JSON which stands for JavaScript Object Notation.<br />

It’s nothing more than the syntax you are seeing below:<br />

var main_object = { object1name: objectvalue, object2name: objectvalue };<br />

You can have more than one object name and value pairs separated by comma. The variable<br />

main_object is an object itself, whose parameters are specified using the JSON notation.<br />

Remember that everything in JavaScript is an object. This means that the objects object1name<br />

and object2name can be variables or functions. In other words, JSON is not limited to basic<br />

data types.<br />

The actual value specified in objectvalue can be any variable like an integer or a string. But it<br />

can also be an array, a function or even another JSON. Again, this is because everything in<br />

JavaScript is an object and the parameters inside a JSON are objects.<br />

As an example let’s take a look at a practical example of acceptable values in a JSON.<br />

var json =<br />

{<br />

animal: “cat”,<br />

legs: 4,<br />

eat: function() { /*eat*/ },<br />

array: [0,1,2,3,4],<br />

json: {<br />

primary: 1,<br />

secondary: 2,<br />

another_array: [“a”, “b”, “c”]<br />

},<br />

final: 7<br />

};<br />

alert(json.json.another_array[1]); //display “b”<br />

50

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!