JavaScript JSON 1
the object "employees" is an array containing three objects
"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter","lastName":"Jones"} ]
name/value pair syntax
"firstName":"John"
Square brackets hold?
arrays
var employees = [ { "firstName":"John", "lastName":"Doe" }, { "firstName":"Anna", "lastName":"Smith" }, { "firstName":"Peter", "lastName":"Jones" } ]; employees[1].firstName + " " + employees[1].lastName;
"Anna Smith"
var employees = [ { "firstName":"John", "lastName":"Doe" }, { "firstName":"Anna", "lastName":"Smith" }, { "firstName":"Peter", "lastName":"Jones" } ]; employees[0]["firstName"] + " " + employees[0]["lastName"];
"John Doe"
var ourStorage = { 'desk': { 'drawer': 'stapler' }, 'cabinet': { 'top drawer': { 'folder1': 'a file', 'folder2': 'secrets' }, 'bottom drawer': 'soda' } } ourStorage.cabinet['top drawer'].folder1;
"a file"
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":"John"
var myStorage = { 'car': { 'inside': { 'glove box': 'maps', 'passenger seat': 'crumbs' }, 'outside': { 'trunk': 'jack' } } }; var gloveBoxContents = myStorage.car.inside["glove box"]; gloveBoxContents;
"maps"
var ourStorage = { 'desk': { 'drawer': 'stapler' }, 'cabinet': { 'top drawer': { 'folder1': 'a file', 'folder2': 'secrets' }, 'bottom drawer': 'soda' } } ourStorage.desk.drawer;
"stapler"
* 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.
* 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.
JSON - Evaluates to JavaScript Objects The JSON format is syntactically identical to the code for creating JavaScript objects. Because of this similarity, instead of using a parser (like XML does), a JavaScript program can use standard JavaScript functions to convert JSON data into native JavaScript objects.
JSON - Evaluates to JavaScript Objects The JSON format is syntactically identical to the code for creating JavaScript objects. Because of this similarity, instead of using a parser (like XML does), a JavaScript program can use standard JavaScript functions to convert JSON data into native JavaScript objects.
JSON Syntax Rules JSON syntax is derived from JavaScript object notation syntax: Data is in name/value pairs Data is separated by commas Curly braces hold objects Square brackets hold arrays
JSON Syntax Rules JSON syntax is derived from JavaScript object notation syntax: Data is in name/value pairs Data is separated by commas Curly braces hold objects Square brackets hold arrays
var myMusic = [ { 'artist':'Jipi Jams', 'title' : 'HooHoo', 'release_year' : 1919, 'formats' : [ 'brick', 'LP', 'cassette'], 'gold' : true } //You can add another record here ];
JSON object
JSON:
JavaScript Object Notation.
var ourStorage = { 'desk': { 'drawer': 'stapler' }, 'cabinet': { 'top drawer': { 'folder1': 'a file', 'folder2': 'secrets' }, 'bottom drawer': 'soda' } } ourStorage.cabinet['top drawer'];
Object { folder1="a file", folder2="secrets"}
Here are the parts. What do you need to put them together? var ourStorage "desk": { "drawer": "stapler" } "cabinet": { "top drawer": { "folder1": "a file", "folder2": "secrets" }, "bottom drawer": "soda" }
Set ourStorage to all objects by enclosing them in curly braces and separating each object with a comma: var ourStorage = { 'desk': { 'drawer': 'stapler' }, 'cabinet': { 'top drawer': { 'folder1': 'a file', 'folder2': 'secrets' }, 'bottom drawer': 'soda' } } ourStorage.desk.drawer;
What is JSON? JSON stands for JavaScript Object Notation JSON is a lightweight data-interchange format JSON is language independent * JSON is "self-describing" and easy to understand
What is JSON? JSON stands for JavaScript Object Notation JSON is a lightweight data-interchange format JSON is language independent * JSON is "self-describing" and easy to understand
JSON is an easier-to-use alternative to...
XML.
properties with spaces must go inside?
brackets & double quotes
JSON names require what? JavaScript names don't.
double quotes
Curly braces hold?
objects
JSON is a syntax for ...
storing and exchanging data.
JSON Uses JavaScript Syntax Because JSON syntax is derived from JavaScript object notation, very little 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:
var employees = [ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter","lastName": "Jones"} ];
return 'tulip' : var myPlants = [ { type: 'flowers', list: [ 'rose', 'tulip', 'dandelion' ] }, { type: 'trees', list: [ 'fir', 'pine', 'birch' ] } ];
var myPlants = [ { type: 'flowers', list: [ 'rose', 'tulip', 'dandelion' ] }, { type: 'trees', list: [ 'fir', 'pine', 'birch' ] } ]; myPlants[0].list[1];
return 'birch' : var myPlants = [ { type: 'flowers', list: [ 'rose', 'tulip', 'dandelion' ] }, { type: 'trees', list: [ 'fir', 'pine', 'birch' ] } ];
var myPlants = [ { type: 'flowers', list: [ 'rose', 'tulip', 'dandelion' ] }, { type: 'trees', list: [ 'fir', 'pine', 'birch' ] } ]; myPlants[1].list[2];
return "Fluffy" : var ourPets = { 'cats': [ 'Meowzer', 'Fluffy', 'Kit-Cat' ], 'dogs': [ 'Spot', 'Bowser', 'Frankie' ] };
var ourPets = { 'cats': [ 'Meowzer', 'Fluffy', 'Kit-Cat' ], 'dogs': [ 'Spot', 'Bowser', 'Frankie' ] }; ourPets.cats[1];
return "Spot" : var ourPets = { 'cats': [ 'Meowzer', 'Fluffy', 'Kit-Cat' ], 'dogs': [ 'Spot', 'Bowser', 'Frankie' ] };
var ourPets = { 'cats': [ 'Meowzer', 'Fluffy', 'Kit-Cat' ], 'dogs': [ 'Spot', 'Bowser', 'Frankie' ] }; ourPets.dogs[0];
Just like JavaScript, JSON objects can contain multiple name/values pairs:
{"firstName":"John", "lastName":"Doe"}