|
JSON |
||||||||
|
Home
Products Product Comparisons Features Screen Shots Downloads Updates Free Licenses Support Solutions FAQ Papers AJAX JSON Editors Java HTML Perl JSP JavaScript PHP VBScript Velocity XML |
JSON stands for JavaScript Object Notation. It is a subset of JavaScript which can be used in place of XML and other data-interchange formats such as comma separated fields and name value pairs in URLs. It is easy for humans and machines to read and write. JSON is based on a subset of JavaScript that is not generally used in scripting. It also one of the features that comes from JavaScript's Lisp background. The specifications for JSON are here. JSON is a text format that can be used to persist object state and to exchange data between applications. JSON consists of objects, members and values. A simple object with one member looks like this:
{ name: value }
In the above example the object has one member that consists of 'name: value'. You could also enter '"name": value' with the name quoted. If you assign that object to a JavaScript variable like this:
var myObject = { name: value };
then you can access the value by using the JavaScript dot
notation JSON values include arrays along with strings, numbers, and the literals 'true', 'false' and 'null'. Values also include objects. In JSON objects are delimited by curly braces, arrays are delimited by brackets and name-value pairs are separated by commas. Here is a more complex example that defines the object for the 'colorvote' data on the AJAX page.
var voteData = { colorvote: {
red : 0,
blue : 0,
green : 1
}
};
You could also create this object using: var voteData = new Object(); voteData.colorvote = new Object(); voteData.colorvote.red = 0; voteData.colorvote.blue = 0; voteData.colorvote.green = 1; Or create this object using eval(), the JavaScript compiler, using a string:
var data = '{ "colorvote": {"red": 0,"blue": 0,"green": 1}}';
var voteData = eval('(' + data + ')');
It is this last form that is useful for saving or exchanging data. However, we also need to be able to create JSON text from objects and to safely parse untrusted data. The eval() function can be used to parse data, but it is dangerous to use eval() with untrusted data since it will parse any legal JavaScript. Second we also need to be able to create and parse JSON text in environments other than JavaScript. Handling JSON dataThe solution for creating and parsing JSON text is to use custom code. This code has already been developed for a number of languages. For a list of available languages (including PHP, Java and JavaScript) see the bottom of the Introducing JSON page. Each of the available libraries provide a way to create and parse JSON text. For JavaScript the methods are parse and stringify and are used like this: var myObject = JSON.parse( JSONtext ); var JSONText = JSON.stringify(myObject); If we apply JSON.stringify() to the voteData from above
we get: |
||||||||
|