We often hear about JSON. But what is this JSON and How it works? JSON stands for JavaScript Object Notation, it is a lightweight format for data exchange. JSON is a subset of the object literal notation of JavaScript that does not require the use of XML. The simplicity of JSON has led to their widespread use, especially as an alternative to XML in AJAX. One of the supposed advantages of JSON over XML as a data exchange format in this context is that it is much easier to write a parser (parser) in JSON.
JSON : Data Types, Syntax and Example
In JavaScript, a JSON text can be analyzed using the function eval ()
, which is fundamental to JSON has been accepted by the AJAX developer community, due to the ubiquity of JavaScript in almost any web browser. In practice, the case for ease of development of analyzers or performance thereof are irrelevant because of the safety issues posed by the use of eval()
and the rise of native XML processing incorporated in the modern browsers.
For that reason, JSON is typically used in environments where the size of the data flow between client and server is vital (hence its use by Yahoo, Google, etc., serving millions of users) when the data source is explicitly trusted and is not important where the XSLT processing does not have to manipulate the data on the client. While it is common to see JSON positioned against XML, is also frequent use of JSON and XML in the same application. For example, a client application that integrates data from Google Maps with weather data in SOAP necessitate support both formats.
---
There is growing support JSON using packages written by third parties. The list of supported languages ??includes ActionScript , C , C + + , C # , ColdFusion , Common Lisp , Delphi , E , Eiffel , Java , JavaScript , ML , Objective-C , Objective CAML , Perl , PHP , Python , Rebol , Ruby , Lua and Visual FoxPro. In December 2005, Yahoo! began to optional JSON support in some of their web services. The term JSON is highly publicized in the media programming, however, is a term described as evil is actually only part of the definition of the ECMA-262 standard which is based on Javascript. Hence, neither Yahoo nor Google use JSON, but LJS. One of the intrinsic qualities of Javascript called LJS (Literal Javascript) facilitates the flow of data or functions for which does not require the function eval()
if the data are transferred as in the case of XML – Everything about data transfer all types, including arrays, booleans, integers, etc. never not require the function eval()
.
The data can be nested, for example, an array of objects is possible. Encoding of JSON is by default UTF-8. Also, UTF-16 and UTF-32 are compatible. JSON recognizes the following data types:
Zero value : is null
Boolean value : is determined by the keywords true and false. These are not character strings. They are therefore not enclosed in quotes.
Number : is a sequence of digits 0 – 9. This sequence can by leaded by a negative sign.
String : begins and ends with double straight quotation marks ( ” )
Array : starts with [ and ends with ] . It contains an ordered list of values, divided by commas, of same or different type. Empty arrays are allowed.
Object : begins with { and ends with }. It contains unordered list of properties, divided by commas. Objects without properties (“empty objects”) are permitted.
Property : consists of a key and a value, separated by a colon ( Name:Abhishek ). The keys of all the properties in an object must be unique and different in pairs. The key is a string . The value is an object, an array, a string, a number or one of the expressions true , false or null .
Using JSON
In theory, it is trivial to parse JSON in JavaScript using the function eval() built-in language. For example:
1 | myObject = eval ('(' + json_datos + ')'); |
In practice, security considerations usually do not recommend using eval on raw data and should be a separate JavaScript to ensure safety. The analyzer provided by JSON.org use eval()
function in its analysis, protecting it with a regular expression so that the function only looks unsafe expressions. An example of JSON data access using XMLHttpRequest is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | http_request var = new XMLHttpRequest (); var url = "https://thecustomizewindows.com/xmlprc.php" // This URL should return JSON data // Download the JSON data from the server. http_request onreadystatechange = handle_json.; http_request open ("GET", url, true).; http_request send (null).; handle_json function () { if (readyState http_request. == 4) { if (status == http_request. 200) { json_data var responseText = http_request.; the_object var = eval ("(" + json_data + ")"); Else {} alert ("There was a problem with the URL."); } http_request = null; } } |
Note that the use of XMLHttpRequest in this example is not compatible with all browsers, because there are syntactic variations for Internet Explorer, Opera, Safari and browsers based on Mozilla.
Tagged With how json works , why json , how jason works , what is json object