.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Articles >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Articles

JavaScript Object Notation (JSON)

Posted By:Lakhan Pal Garg       Posted Date: September 12, 2009    Points: 25    Category: Javascript    URL: http://www.dotnetspark.com  

JavaScript Object Notation (JSON)

(JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a language independent text format which is fast and easy to understand. JavaScript Object Notation (JSON) is a text format for the serialization of structured data. File extension for json file is *.json

The application/json is media type for JavaScript Object Notation (JSON). JSON can represent four primitive types:
. Strings (double-quoted Unicode with backslash escaping)
. Numbers (integer, real, or floating point)
. Booleans (true and false)
. Null
And two structured types:
. Objects (collection of key:value pairs, comma-separated and enclosed in curly braces)
. Arrays (an ordered sequence of values, comma-separated and enclosed in square brackets)

A JSON text is a serialized object or array.

JSON-text = object / array

These are the six structural characters:

begin-array [ left square bracket

begin-object { left curly bracket

end-array ] right square bracket

end-object } right curly bracket

name-separator : colon

value-separator , comma

The following example shows the JSON representation of an object that describes a person. The object has string fields for first name and last name, contains an object representing the person's address, and contains a list of phone numbers (an array)

"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
"phoneNumbers": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }

The equivalent for the above in XML:

21 2nd Street
New York

212 555-1234

646 555-4567

JSON is like XML because:
1. They are both 'self-describing' meaning that values are named, and thus 'human readable'
2. Both are hierarchical. (i.e. You can have values within values.)
3. Both can be parsed and used by lots of programming languages
4. Both can be passed around using AJAX (i.e. httpWebRequest)

JSON is Unlike XML because:
1. XML uses angle brackets, with a tag name at the start and end of an element: JSON uses squiggly brackets with the name only at the beginning of the element.
2. JSON is less verbose so it's definitely quicker for humans to write, and probably quicker for us to read.
3. JSON can be parsed trivially using the eval() procedure in JavaScript
4. JSON includes arrays {where each element doesn't have a name of its own}
5. In XML you can use any name you want for an element, in JSON you can't use reserved words from JavaScript

What is good about JSON?
When you're writing Ajax stuff, if you use JSON, then you avoid hand-writing xml. This is quicker.
Again, when you're writing Ajax stuff, which looks easier? The XML approach or the JSON approach:
The XML approach:
1. Bring back an XML document
2. Loop through it, extracting values from it
3. Do something with those values, etc,
The JSON approach:
1. Bring back a JSON string.
2. 'eval' the JSON

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend