JSON
JSON (JavaScript Object Notation) is a format for data exchange between the browser and the server.
Its syntax is similar to an associative array in JavaScript.
JSON sample provided on the JSON tutorial on xul.fr.
{
"menu": "Files",
"commands":
[
{
"title": "New",
"action":"CreateDoc"
},
{
"title": "Open",
"action": "OpenDoc"
},
{
"title": "Close",
"action": "CloseDoc"
}
]
}
This file is the representation of a menu, the site provides a demonstration of its use.
How to use JSON
A JSON file is read on the server through a parser. A parser exists for most common programming languages.
In a web page, simply give the contents of the file as an argument to the eval() function to return an array directly usable by JavaScript.
Alternative formats
JSON or XML are two formats used to exchange data with a server.
Other formats include YAML, more comprehensive but also more complex and Protocol Buffers created by Google for its servers.
This format fits better in languages other than JavaScript because, from a prototype that describes the data (that would be lines of menu in our example), the compiler creates classes in C or Java to access the data. But it is better for static data.
More informations
- Json.org. The site provide links to each parser.
(c) 2008-2009 Scriptol.com