JSON is a data format that stands for Javascript Object Notation. It's used as data interchange protocol between different languages or to create instances of Javascript objects.
It stores data in the key-value format.
: are used to separate a key from a value
, are used to separate items
{} are used to represent objects
[] are used to represent arrays
This is an example of JSON:
var student = { "name":"Tim", "surname":"Gordon", "hobbies": ["football","music","travelling"], "showName": function() { alert(this.name); }, "locale": { "address": "14, Piccadilly Circus", "city": "London" } } student.showName();
A JSON is serialized in a string format when sent on the network and deserialized when received.
JSON.stringify() is used to serialize. JSON.parse() is used to deserialize.
This is an example:
var student = '{"name":"Tim","surname":"Gordon"}' var myObjStudent = JSON.parse(student); // Serialize alert(myObjStudent["name"]); // I'm using the object as associative array alert(myObjStudent.name); // Other way to read the content var studentClone = JSON.stringify(myObjStudent); // Deserialize alert(studentClone);
Note that eval() is not safe to deserialize, because it can execute any Javascript command.
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.