JSON (Javascriptobject Notation) is a simple data format that is lighter than XML. JSON is a native JavaScript format, which means that working with JSON data in JavaScript does not require any special APIs or toolkits.
The rules of JSON are simple: an object is an unordered collection of "name/value pairs". An object ends with "{" Starting with "}". Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).
A simple example:
var user =
{
"username": "Andy",
"Age": 20,
"Info": {"tel": "123456", "cellphone": "98765"},
"Address":
[
{"City": "Beijing", "postcode": "222333"},
{"City": "NewYork", "Postcode": "555666"}
]
}
alert (user.username);
alert (user.age);
alert (User.info.cellphone);
alert (user.address[0].city);
alert (User.address[0].postcode);
String to JSON data object:
var user = ' {' username ': ' Andy '} ';
var obj = eval (' (' +user+ ') ');
alert (obj.username);