Presentation1 JSON
Presentation1 JSON
JavaScriptObjectNotation
What is JSON ?
JSON is a syntax for storing and exchanging
data.
JSON is an easier-to-use alternative to XML.
JSON is a lightweight data-interchange
format.
JSON is language independent*.
JSON is "self-describing" and easy to
understand.
JSON uses JavaScript syntax, but the JSON format is text only, just
like XML.
(Text can be read and used as a data format by any programming
language.)
{"employees":[
{"firstName":"John","lastName":"Doe"},
{"firstName":"Anna","lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]}
The following XML example also defines an employees object with 3
employee records:
<employees>
<employee>
<firstName>John</firstName><lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName><lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName><lastName>Jones</lastName>
</employee>
Both JSON and XML can be parsed and used by lots of programming
languages
JSON is shorter
The biggest difference is: XML has to be parsed with an XML parser, JSON
can be parsed by a standard JavaScript function.
JSON Introduction
Why JSON?
For AJAX applications, JSON is faster and easier than XML:
Using XML
Using JSON
JSONSyntax
The JSON syntax is a subset of the
JavaScript syntax.
Example
"firstName":"John
Note:
JSON Values
JSON values can be:
A number (integer or floating point)
A string (in double quotes)
A Boolean (true or false)
An array (in square brackets)
An object (in curly braces)
null
JSON Objects
JSON objects are written inside curly braces.
Just like JavaScript, JSON objects can
contain multiple name/values pairs:
Example:
{"firstName":"John","lastName":"Doe"}
JSON Arrays
JSON arrays are written inside square brackets.
Just like JavaScript, a JSON array can contain
multiple objects:
Example:
"employees":[
{"firstName":"John","lastName":"Doe"},
{"firstName":"Anna","lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]
In the example above, the object "employees" is an array containing three
objects. Each object is a record of a person (with a first name and a last
name).
JSON Files
The file type for JSON files is ".json"
The MIME type for JSON text is
"application/json"
JSONHow To
A common use of JSON is to read data from a web server, and display the
data in a web page.
For simplicity, this can be demonstrated by using a string as input (instead
of a file).
Using eval()
Older browsers without the support for the JavaScript function
JSON.parse() can use the eval() function to convert a JSON text
into a JavaScript object:
Example
varobj = eval ("("+ text +")");
<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<p id="demo"></p>
<script>
var txt = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';
var obj = eval ("(" + txt + ")");
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
</script>
</body>
JSONHttp Request
A common use of JSON is to
read data from a web server,
and display the data in a web
page.