1. Introduction
JSON: Java Script Object Notation.
JSON is smaller and faster then XML , it is also parsed easier
JSON syntax used to store and exchange information
JSON example :JSON is smaller and faster then XML , it is also parsed easier
JSON syntax used to store and exchange information
{
"students": [
{ "firstName":"Jim" , "lastName":"Deer" },
{ "firstName":"Kim" , "lastName":"Wild" },
{ "firstName":"Jana" , "lastName":"Jampolski" }
]
}
"students": [
{ "firstName":"Jim" , "lastName":"Deer" },
{ "firstName":"Kim" , "lastName":"Wild" },
{ "firstName":"Jana" , "lastName":"Jampolski" }
]
}
student is an array of three objects
XML similarities
- JSON is simple text
- JSON is "self-describing" (human readable)
- JSON can be parsed by JavaScript
- JSON data can be transported using AJAX
- JSON is hierarchical (values within values)
XML dissimilarities
- No end tag
- Shorter
- Uses arrays
- Quicker to write and read
- Can be parsed using built-in JavaScript eval()
- No reserved words
Why JSON?
For AJAX applications, JSON is faster and easier than XML:
Using XML
Using XML
- Load an XML document
- Use the XML DOM to read the document
- Extract values and store in variables
- Get a JSON string
- eval() the JSON string
2. JSON Syntax
JSON syntax is a subset of JavaScript syntax
JSON Syntax Rules :
- Data is in name/value pairs
- Data is separated by comma
- Square brackets holds arrays
- Curly brackets holds objects
JSON Name/Value Pairs
JSON data is written as name/value pairs.
A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:
"firstName" : "Anna"
This is equals to the JavaScript statement:
firstName = " Anna "
JSON Values
JSON values can be
- A number (integer or floating point)
- A string (in double quotes)
- A Boolean (true \ false)
- An array (in square brackets)
- An object (in curly brackets)
- null
JSON Objects
JSON objects are written inside curly brackets,
Objects can contain multiple name/values pairs:
{ "firstName":"Anna" , "lastName":"Long" }
This is equals to the JavaScript statements:
firstName = " Anna "
lastName = " Long "
JSON Arrays
JSON arrays are written inside square brackets.
An array can contain multiple objects:
{
"students": [
{ "firstName":"Pete" , "lastName":"Cool" },
{ "firstName":"Nathan" , "lastName":"Krasney" },
{ "firstName":"James" , "lastName":"Kid" }
]
}
In the example above, the object "students " is an array containing three objects. Each object is a record of a student (with a first name and a last name).
JSON Uses JavaScript Syntax
Because JSON uses JavaScript syntax, no extra software is needed to work with JSON within JavaScript.With JavaScript you can create an array of objects and assign data to it like this:
Example
var students = [
{ "firstName":"Pete" , "lastName":"Cool" },
{ "firstName":"Nathan" , "lastName":"Krasney" },
{ "firstName":"James" , "lastName":"Kid" }
];
The first entry in the JavaScript object array can be accessed like this:
students[0].lastName;
The returned content will be:
Cool
Nathan