JS JSON Introduction

 

- Info:

- JSON stands for JavaScript Object Notation.

- JSON is a subset of the "Object literal notation of JavaScript".

- JSON is set of rules which define how to combine hashes and arrays to defining data structures as simple text.

- Hash/Object is defined inside {} like: { "apple":100, "orange":200 }. This construct is called Object literal.

- Array is defined inside [] like: [ "apple", "orange"]. This construct is called Array literal.

- These can then be combined to define complex structures:

var jsonObj = { "pie" : ["cherry","meringue","apple"],

"heated" : true,

"personEatingPie": {"firstname":"Jonathan", "lastname":"Snook" }

}

- JSON structures are like property files but where properties can be nested, it also supports list of values through arrays.

- JSON homepage is at http://www.json.org/

 

- Links:

- http://secretgeek.net/bought_youmustget.asp

- http://www.json.org/js.html

- http://www.dyn-web.com/tutorials/obj_lit.php

- http://snook.ca/archives/javascript/json_is_a_subse

- http://www.dyn-web.com/tutorials/obj_lit.php

 

- Common JSON Experiance:

- Two months ago you'd never heard of JSON.

- One month ago you'd heard the term but paid no attention.

- One week ago you'd heard it mentioned a few times and started to think: "right... some more crap to learn".

- Today you woke up with an alarm bell ringing in the back of your mind that said:

"WHAT THE BLOODY HELL IS THIS JSON THING AND WHY IS IT EVERYWHERE ALL OF SUDDEN!"

- This was taken from http://secretgeek.net/json_3mins.asp.

 

- Example:

- This example defines JSON object with single name:value pair.

- Name is "skills".

- Value is another object with two name:value pairs.

- Names are "web" and "database".

- Value of "web" is array with two objects.

- Value of "database" is array with single object.

- Each of the objects inside arrays contain two name:value pairs.

 

Example

{"skills":{ "web": [ {"name": "html", "years": "5", },

{"name": "css" , "years": "3" } ],

"database":[ {"name": "sql" , "years": "7" } ]

}

}

 

- Object/Hash:

- An object begins with '{' and ends with '}'.

- An object is an unordered set of name:value pairs called properties.

- Pairs are separated by ','.

- This means that Hash would be a better name for a JSON object.

 

example

var objectPrices = { "apple":100, "orange":200 }

 

- Array:

- An array begins with '[' and ends with ']'.

- An array is an ordered set values.

- Values are separated by ','.

 

example

var arrayItems = { "apple", 100, true, false, null, objectPrices, arraySome }

 

- Value:

- Value can be: string, number, true, false, null, objectPrices, arraySome.

 

example

var arrayItems = { "apple", 100, true, false, null, objectPrices, arraySome }

- String:

- String is a sequence of zero or more Unicode characters.

- String is wrapped in double quotes.

- String uses backslash '\' for escaping characters.

- A character is represented as a single character string.

 

- Number:

- Number must be in decimal format.

- Octal and hexadecimal formats are not supported.

 

- Name:

- Name is defined as String.