INTRODUCING JSON
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read n write. It is easy for machines to parse n generate. JSON is an ideal data – interchange language.
JSON : The Fat-Free alternative to XML
XML – Provides two enormous advantages as a data representation language.
- It is a Text-based
- It is a position-independent.
Unfortunately, Xml is not well suited to data-interchange, much as a wrench is not well suited to driving nails. It carries a well baggage, and it doesn’t match to the data model to the most of the programming languages. There is another text notation that has all of the advantageous as the XML, but it is much better suited as to the data interchange. That notation is Java Script Object Notation (JSON).
Let us compare the XML and JSON on the attributes that xml community considers as important
XML is simpler than SGML, but JSON is simpler than XML. JSON has much smaller than grammar and maps more directly onto the data structures used in programming languages
JSON is not extensible because it doesn’t need to be. JSON is not a Document markup language. JSON is not a mark up language, so it is not necessary to define the tags or attributes to represent data in it.
It is optional as XML.
JSON is at least as open as XML, perhaps more because it is not center of corporate/political standardization struggles.
Advantages of XML
Xml is human readable
- JSON is much easier for human to read than xml. It is easy to write to and machine understandable.
XML can b used as an exchange format to enable users to move their data applications between similar applications.
- The same is true for JSON.
XML is easily processed because the structure of the data is simple and standard
- JSON is processed more easily bcz its structure is simple.
Many Views of the one data
- JSON does not provide any display capabilities bcz it is not a document mark up language.
Complete Integration of all data bases and formats
XML documents can contain any imaginable data type-from classical data like text and numbers, or multimedia objects such as sounds, to active formats like Java Applets and Active X Controls.
JSON doesn’t have a < [CDATA []]>the feature, so it is not well suited to act as carrier of sounds or images or other large binary payloads. JSON is optimized for data. Besides, delivering executable programs in data-interchange system could introduce dangerous security problems.
Open and extensible
XML’s is a one-of-a-kind open structure allows u to add other state of art element vn needed. This means always u can adapt ur system to embrace industry-specific vocabulary.
Those Vocabularies can b automatically converted to JSON, making migration from XML to JSON very straightforward.
XML and JSON has this in common Self-Describing data and use Unicode
JSON is built on two structures:
· A collection of Name/ValuePairs.In various languages, this is realized as an object, record, dictionary, struct, hashtable, keyed list, or associative array.
· An orders list of values. In most languages this is realized as an array, vector, list or sequence.
These are universal data structures. Virtually all modern programming languages support them in one form or another .It makes sense that a data format that is interchangeable with programming languages is also based on these structures.
In JSON they take on these forms:
An object is an understand set of name/value pairs. An object begins with {and ends with}. Each is name is followed by: and the name/value pair is separated by, (comma).
An Array is an order collection of values. An Array begins with [and ends with] .
Values are separated by, comma.
ARRAY
A Value can be a string in double quotes, or a number, or true or false or null or an object or an array. These structures can be nested.
A Number is very much like C or Java number, except that the octal and hexadecimal are not used.
White Spaces can be inserted between any pair of tokens. Excepting any few encoding details, that completely describes the language.
JSON.NET
This provides a .Net API for easily reading JSON objects to the server from the browser and then writing them back in the response.
Familiar API
Developer experienced with .NET XML API vl have no trouble in using Json.Net which like the XML API has readers and writers for working with data. The Fat- free alternative to xml
Fast Performance
Json.net uses fast, forward only readers and writers; maximizing the performance while minimizing the memory use.
http://www.newtonsoft.com/products/json/quickstart.aspx
JSON is a subset of object literal notation of JavaScript. Since JSON is a subset of the JavaScript, it can be used in the language vth no fuss or muss.
Var myJSONObject = {“bindings” :[{ “ircEvent”: “PRVMSG”, “method”: “new URI”,” regx”: “^http ://.*”}, {“ircEvent”: “PRVMSG”, “method”: “delete URI”,” regx”: “^delete.*”}, {“ircEvent”: “PRVMSG”, “method”: “delete URI”,” regx”: “^random.*”}]};
In this example an object is created containing a single number “bindings”, vch contains an array vth the three objects containing “ircEvent”; “method” and” regx” members.
Members can b retrieved using dot or subscript operators.
myJSONObject.bindings [0].method // “new URI”.
To Convert a JSON text into an object, use the eval () function. Val () invokes JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler VL exactly parses the text and produces an object structure.
Var myObject =eval (‘(‘+myJSONtext+’)’);
The eval function is very fast. However, it can compile and execute any JavaScript program, so there can b security issues. The use eval is indicated vn the source is trusted. This is commonly the case in web server is providing both the base page and the JSON data .There r cases vr there not trusted. In particular, client should never be trusted.
When security is concern its better to use a JSON parser. A JSON will only recognize JSON text and much safer.
Var myObject = myJSONtext.parseJSON (filter);
The optional filter parameter is a function vch VL b called every key at every level for the final result. Each value VL b replaced by the result of filter function. This
Can b used to reform generic objects into instances, or to transform date strings in to date objects
myData = text.parseJSON (function (key, value)) {return key.indexof (‘Date’) >=0? New Date (value): value;
A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support data structures, so b careful to not give cyclical structures to the JSON stringifier.
Var myJSONtext = myObject.toJSONString ();