What is JSON?
JSON , which stands for “JavaScript Object Notation”, is a lightweight data-interchange format that is easy for humans to read and write, and for machines to parse and generate. JSON is based on the Object Notation of the JavaScript language. However, it does not require JavaScript to read or write because it is a text format that is language independent,but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, PERL, Python, and many others.
JSON’s simplicity has resulted in its widespread use, especially as an alternative to XML in AJAX. One of the claimed advantages of JSON over XML as a data interchange format in this context is that it is much easier to write a JSON parser. In JavaScript itself, JSON can be parsed trivially using the eval() procedure. This was important for the acceptance of JSON within the AJAX programming community because of JavaScript’s ubiquity among web browsers.
What is Object Notation in JavaScript?
As a simple example, Object Notation can be expressed in the following format:
var obj = {
// string
a : 'sampleValue',
// array
b : [obj1,obj2,'three',4,obj5],
// function
c : function() {
var bang = this.a;
}
}
In the above example, a, b, and c are the names, and their values are expressed immediately following the colon up until the following name (excluding the final value - in which the value just goes until the end of the object). Each pair is considered a member.
Names
Names can be anything except one of the JavaScript reserved keywords - which come to find out can actually be quite lengthy. Names also follow a few syntax rules which include not being allowed to start with a number; the entire name may not include any special characters except an underscore or dollar sign; and it cannot be a duplicate of a previously used name within the same object.
Values
Values can be expressed as a string, number, object, array, boolean (true,false), or null. To see a full explanation of the syntax rules, see JSON Website.
Reasons to use JSON
- JSON is easy.
- If you’re familiar with writing classes in PHP, then you’ll most definitely be comfortable with writing JavaScript in Object Notation.
- JSON is nothing more than name : value pairs assigned within an object.
- JSON is easy to understand because if written well, it’s a self-documenting structure.
- JSON is fast.
- JSON organizes the ugly mess of procedural programming. Imagine having more than one init function.
- Your co-workers will love you for writing in JSON because it will most likely not conflict with their scripts that are being called within the same web documents.
Tags: javacsript, JavaScript, JSON, notation, object, XML
Related:
Posted in JSON, JavaScript. 1,983 views
Responses are currently closed, but you can trackback from your own site.