1 / 12

JSON

JSON. 2017, Fall Pusan National University Ki-Joune Li. JSON – Basic Concepts. Browser. Web Server. JavaScript Object. JSON. From JavaScript Object to JSON.

sparsley
Download Presentation

JSON

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. JSON – Basic Concepts 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; • JSON (JavaScript Object Notation) • A Format (or Syntax) to store and exchange object data • Text data

  3. JSON – Basic Concepts 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; • JSON (JavaScript Object Notation) • Storing data in local storage as text data

  4. Syntax { "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"; • 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

  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 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>"; } • 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

  7. 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; • Nested JSON Objects • Values in a JSON object can be another JSON object.

  8. 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) { • JSON Array Objects • Values in a JSON object can be another JSON object.

  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 {"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> Both JSON and XML can be used to receive data from a web server.

  12. JSON – XML vs. JSON • JSON is like 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

More Related