Introduction to JSON
What is JSON?
- JSON stands for JavaScript Object Notation
- JSON is lightweight text-data interchange format
- JSON is language independent
- JSON is "self-describing" and easy to understand
Example
{
"id":1,
"name":"Anoop",
"billingAddress":[{"city":"Bangalore"}]
}
JSON is like XML
- JSON is plain text
- JSON is "self-describing" (human readable)
- JSON is hierarchical (values within values)
- JSON can be parsed by JS
JSON is unlike XML
- No end tag
- Shorter
- Quicker to read and write
- Can be parsed using built-in JS parse()
- Uses arrays
- No reserved words
Data on a web page
- Using XML
+ Fetch an XML document
+ Use the XML DOM to loop through the document
+ Extract values and store in variables
- Using JSON
+ Fetch a JSON string
+ Parse() the JSON string to get javascript objects
JSON Syntax
- Data is in name/values pairs
- Data is seperated by commas
- Curly braces hold objects
- Square brackets hold arrays
JSON object
- JSON object are written inside curly brackets
- Object can contain multiple name/values pairs separated by comma
- Names in objects are supposed to be unique
JSOn and other language
- APIs are available for all contemporary web progamming languages
- All browers today are JSON-aware
- For server-side, specific APIs are in Java, .Net, PHP, etc.