Presentation is loading. Please wait.

Presentation is loading. Please wait.

2017, Fall Pusan National University Ki-Joune Li

Similar presentations


Presentation on theme: "2017, Fall Pusan National University Ki-Joune Li"— Presentation transcript:

1 2017, Fall Pusan National University Ki-Joune Li
JSON 2017, Fall Pusan National University Ki-Joune Li

2 JSON – Basic Concepts JSON (JavaScript Object Notation)
A Format (or Syntax) to store and exchange object data Text data Browser Web Server JavaScript Object JSON From JavaScript Object to JSON var myObj = { "name":"John", "age":31, "city":"New York" }; var myJSON = JSON.stringify(myObj); window.location = "demo_json.php?x=" + myJSON; From JSON Object to JavaScript var myJSON = '{ "name":"John", "age":31, "city":"New York" }'; var myObj = JSON.parse(myJSON); document.getElementById("demo").innerHTML = myObj.name;

3 JSON – Basic Concepts JSON (JavaScript Object Notation)
Storing data in local storage as text data JavaScript Program JavaScript Object JSON Local Storage Storing data: myObj = { "name":"John", "age":31, "city":"New York" }; myJSON = JSON.stringify(myObj); localStorage.setItem("testJSON", myJSON); Reading data text = localStorage.getItem("testJSON"); obj = JSON.parse(text); document.getElementById("demo").innerHTML = obj.name;

4 Syntax JSON object in JavaScript Use JavaScript Syntax
Data is in name/value pairs Data is separated by commas Curly braces hold objects Square brackets hold arrays JSON object in JavaScript JSON object is accessible in JavaScript { "name":"John",  "age":30,  "car":null  } var person = { "name":"John", "age":31, "city":"New York" }; var x=person.name; var y=person["name"]; // named index var person["name"]="Guilbert";

5 JSON – Data Types and Values
JSON values must be one of the following types a string (should be always doubl-quoted): {"name":"John" } a number:  {"name":"John" } an object (JSON object): { "employee":{"name":"John", "age":30, "city":"New York" } } an array: {"employees":[ "John", "Anna", "Peter" ] } a boolean: {“married”:true} null: { “middlename”: null} JSON file: .json

6 JSON – Object Accessing JSON Object within JavaScript JSON Object
JSON objects are surrounded by curly braces {}. JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon. Each key/value pair is separated by a comma. Accessing JSON Object within JavaScript myObj = { "name":"John", "age":30, "car":null }; x = myObj.name; x = myObj["name"]; myObj = { "name":"John", "age":30, "car":null }; for (x in myObj) {     document.getElementById("demo").innerHTML += myObj[x] + "<br>"; }

7 JSON – Object Nested JSON Objects
Values in a JSON object can be another JSON object. myObj = {     "name":"John",     "age":30,     "cars": {         "car1":"Ford",         "car2":"BMW",         "car3":"Fiat"     }  } x = myObj.cars.car2; //or: x = myObj.cars["car2"]; myObj.cars["car2"] = "Mercedes"; delete myObj.cars.car2;

8 JSON – Object JSON Array Objects
Values in a JSON object can be another JSON object. <!DOCTYPE html> <html> <body> <p>Loopin through an array using a for loop:</p> <p id="demo"></p> <script> var myObj, i, x = ""; myObj = { "name":"John", "age":30, "cars":[ "Ford", "BMW", "Fiat" ] }; for (i = 0; i < myObj.cars.length; i++) { x += myObj.cars[i] + "<br>"; } document.getElementById("demo").innerHTML = x; </script> </body> </html> for (i in myObj.cars) {

9 JSON – Object myObj = {     "name":"John",     "age":30,     "cars": [         { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },         { "name":"BMW", "models":[ "320", "X3", "X5" ] },         { "name":"Fiat", "models":[ "500", "Panda" ] }     ]  }

10 JSON – Parse and Stringify
var obj = { "name":"John", "age":30, "city":"New York"}; var myJSON = JSON.stringify(obj); Convert JavaScript Object to JSON: Stringify Browser Web Server JavaScript Object JSON Convert JSON to JavaScript Objects: parsing var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');

11 JSON – XML vs. JSON Both JSON and XML can be used to receive data from a web server. {"employees":[     { "firstName":"John", "lastName":"Doe" },     { "firstName":"Anna", "lastName":"Smith" },     { "firstName":"Peter", "lastName":"Jones" } ]} <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> </employees>

12 JSON – XML vs. JSON JSON is like XML JSON is unlike XML
Both JSON and XML are "self describing" (human readable) Both JSON and XML are hierarchical (values within values) Both JSON and XML can be parsed and used by lots of programming languages Both JSON and XML can be fetched with an XMLHttpReques JSON is unlike XML JSON doesn't use end tag JSON is shorter JSON is quicker to read and write JSON can be included within JavaScript (e.g. object, arrays) XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function JSON.parse


Download ppt "2017, Fall Pusan National University Ki-Joune Li"

Similar presentations


Ads by Google