JSON (Copied from and from Prof Da Silva)www.w3schools.com Week 12 Web site:

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Copyright © Steven W. Johnson
Sue Wills July Objects The JavaScript language is completely centered around objects, and because of this, it is known as an Object Oriented Programming.
SE 480: Client Side Scripting Languages Week 10: Ajax Data Sources Copyright © Steven W. Johnson October 1, 2014.
JSON Valery Ivanov.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Using Data Types (No Audio Component) © Dr. David C. Gibbs
JSON IDU0075 Sissejuhatus veebiteenustesse.  JSON stands for JavaScript Object Notation  JSON is lightweight text-data interchange format  JSON is.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
JavaScript, Third Edition
15-Jul-15 JSON. JSON example “JSON” stands for “JavaScript Object Notation” Despite the name, JSON is a (mostly) language-independent way of specifying.
JSON (JavaScript Object Notation).  A lightweight data-interchange format  A subset of the object literal notation of JavaScript (or ECMA-262).  A.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
JSON The Fat Free Alternative to XML. Data Interchange The key idea in Ajax. An alternative to page replacement. Applications delivered as pages. How.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Data Types.
RESTful applications Norman White. REST Representational state transfer Key concepts – Client Server architecture built on transferring resources between.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
2440: 211 Interactive Web Programming Expressions & Operators.
Copyright © Curt Hill Sounds, Resource Packs, JSON What more would you want?
Introduction to JavaScript Gordon Tian
Serialization. Serialization is the process of converting an object into an intermediate format that can be stored (e.g. in a file or transmitted across.
JSON Java Script Object Notation Copyright © 2013 Curt Hill.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
CS346 Javascript -3 Module 3 JavaScript Variables.
Strings, output, quotes and comments
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Internet & World Wide Web How to Program, 5/e. 2.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
JSON – Java Script Object Notation. What is JSON JSON is a data interchange format Interactive Web 2.0 applications, no more use page replacement. Data.
AJAX. Ajax  $.get  $.post  $.getJSON  $.ajax  json and xml  Looping over data results, success and error callbacks.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:
Java Script. introduction Today’s web sites need to go much beyond HTML. browsing through a web site, to actually interact with the web site. The web.
OVERVIEW AND PARSING JSON. What is JSON JavaScript Object Notation Used to format data Commonly used in Web as a vehicle to describe data being sent between.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
JSON. JSON as an XML Alternative JSON is a light-weight alternative to XML for data- interchange JSON = JavaScript Object Notation It’s really language.
JavaScript and Ajax Week 10 Web site:
JSON JavaScript Object Notation Douglas Crockford Yahoo! Inc.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
XML & JSON. Background XML and JSON are to standard, textual data formats for representing arbitrary data – XML stands for “eXtensible Markup Language”
JQuery, JSON, AJAX. AJAX: Async JavaScript & XML In traditional Web coding, to get information from a database or a file on the server –make an HTML form.
11 jQuery Web Service Client. 22 Objectives You will be able to Use JSON (JavaScript Object Notation) for communcations between browser and server methods.
WELCOME MIDHUN SUDHAKAR twitter.com/midhunopus in.linkedin.com/pub/midhunsudhakar/86/a65/a9.
Storing Data.
>> Introduction to JavaScript
Exporting and Importing Data
JSON Crash Course Traversy Media.
Exporting and Importing Data
Database Systems Week 12 by Zohaib Jan.
Scope, Objects, Strings, Numbers
Consuming Java Script Object Notation (JSON) feeds
JavaScript Object Notation
JSON Object and JSON Schema
Session V HTML5 APIs - AJAX & JSON
Built in Fairfield County: Front End Developers Meetup
HTML Level II (CyberAdvantage)
HTML5 AJAX & JSON APIs
2017, Fall Pusan National University Ki-Joune Li
JavaScript What is JavaScript? What can JavaScript do?
JavaScript What is JavaScript? What can JavaScript do?
Department of Computer Science Cal State East Bay, Hayward, CA
Lecture 5- Semi-Structured Data (XML, JSON)
Semi-Structured Data (XML, JSON)
JSON: JavaScript Object Notation
Web Client Side Technologies Raneem Qaddoura
Presentation transcript:

JSON (Copied from and from Prof Da Silva) Week 12 Web site:

JavaScript Review - Array Create JavaScript Array: var js_ary = ["John", "Doe"]; OR var js_ary = new Array(); js_ary[0] = "John"; js_ary[1] = "Doe"; Display JavaScript Array: document.write(" ", js_ary[0], " ", js_ary[1], " "); _array.html _array.html

JavaScript Review - Object Create JavaScript Object: var js_obj = { firstName : "John", lastName : "Doe" }; OR var js_obj = new Object(); js_obj.firstName = "John"; js_obj.lastName = "Doe“; Display JavaScript Object: document.write(" ", js_obj.firstName, " ", js_obj.lastName, " "); Display JavaScript Object using Associative Array: document.write(" ", js_obj["firstName"], " ", js_obj["lastName"], " "); t.html t.html

JavaScript Object embedded in Array Create JavaScript Array with object inside: var js_ary = [ { firstName : "Joe" }, { lastName : "Doe" } ]; Display JavaScript Array: document.write(" ", js_ary[0].firstName, " ", js_ary[1].lastName, " "); Use Associative Array for the Object: document.write(" ", js_ary[0]["firstName"], " ", js_ary[1]["lastName"], " "); _object_embedded_in_array.html _object_embedded_in_array.html

JavaScript Array embedded in Object Create JavaScript Object with Array inside: var js_obj = { firstName : ["John"], lastName : ["Doe"] }; Display JavaScript Object: document.write(" ", js_obj.firstName[0], " ", js_obj.lastName[0], " "); Use Associative Array for the Object: document.write(" ", js_obj["firstName"][0], " ", js_obj["lastName"][0], " "); ascript_array_embedded_in_object.html ascript_array_embedded_in_object.html

What is JSON? JSON is short for JavaScript Object Notation. JSON is a syntax for storing and exchanging data. JSON is an easier-to-use alternative to XML.

Characteristics of JSON Lightweight, text-based open standard for data interchange It has been extended from the JavaScript language The extension of the file is.json The internet media type is application/json and the uniform type identifier is public.json It is used when writing JavaScript based application which includes browser extension and websites It is used for transmitting structured data over network connection Primarily used to transmit data between server and web application Easy to read and write and language independent Note: Although JSON is an extension of JavaScript, it is also available in many different languages such as: C, Ruby, Python, etc.

Sample JSON – three employee records {"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ]}

Sample XML – three employee records John Doe Anna Smith Peter Jones

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 Similar to XML Both JSON and XML is "self describing" (human readable) Both JSON and XML is 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 XMLHttpRequest

JSON Different from XML JSON doesn't use end tag JSON is shorter JSON is quicker to read and write JSON can use arrays The biggest difference is:  XML has to be parsed with an XML parser, JSON can be parsed by a standard JavaScript function.

Why JSON? For AJAX applications, JSON is faster and easier than XML: 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  JSON.Parse the JSON string

JSON Syntax JSON syntax is a subset of the JavaScript object notation syntax:  Data is in name/value pairs  Data is separated by commas  Curly braces hold objects  Square brackets hold arrays

JSON Data (Name and Value pair) 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"

JSON Value JSON values can be:  A number (integer or floating point)  A string (in double quotes)  A Boolean (true or false)  An array (in square brackets)  An object (in curly braces)  null

JSON Data Types TypeDescription NumberDouble-precision floating-point format StringDouble-quoted Unicode with backslash escaping BooleanTrue or False ArrayOrdered sequence of values (values = string, number, true or false, null, etc) WhitespaceUse between any pair of tokens ObjectUnordered collection of key:value pairs Nullempty

JSON Number Octal and hexadecimal formats are not used No NaN or Infinity is used in Number Syntax: var json-object-name = {string: number_value, ……} Example: var obj = {marks: 97}

JSON String Sequence of zero or more double quoted characters with backslash escaping Syntax: var json-object-name = {string: number_value, ……} Example: var obj = {name: ‘Joseph’}

JSON String Types TypeDescription "Double quote \Reverse solidus /Solidus bBackspace fForm feed nNew line rCarriage return tHorizontal tab uFour hexadecimal digits

JSON Boolean Includes true or false values Syntax: var json-object-name = {string: true/false, ……} Example: var obj = {name: ‘Josh’, marks: 97, grad: true}

JSON Array Begins with [ and ends with ]. Values are separated by, (comma). Indexing canstart at 0 or 1 Syntax: [value, ………] Example: { “books”: [{“language”:”C”, “edition”:”first”}, {“language”:”C++”, “edition”:”third”}]}

JSON Whitespace Can be inserted between any pair of tokens Syntax: {string: “ “,……} Example: var a = “ bold”; var b = “ lab”

JSON null It means empty type Syntax: null Example: var i = null;

JSON Object JSON objects are written inside curly braces. Just like JavaScript, JSON objects can contain multiple name/values pairs: {"firstName":"John", "lastName":"Doe"}

Sample JSON Object JSON Object Rules:  Enclosed in curly braces.  Each name followed by : (colon) and name/value pairs separated by, (comma).  Keys must be strings and should be different from each other. Create JSON Object: var json_obj = { "firstName" : "John", "lastName" : "Doe" }; Display JSON Object: document.write(" ", json_obj.firstName, " ", json_obj.lastName, " "); Use Associative Array for the Object: document.write(" ", json_obj["firstName"], " ", json_obj["lastName"], " ");

JSON Arrays JSON arrays are written inside square brackets. Just like JavaScript, a JSON array can contain multiple objects: "employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter","lastName":"Jones"} ] In the example above, the object "employees" is an array containing three objects. Each object is a record of a person (with a first name and a last name).

JSON Object with Array Create JSON Object: var json_obj = { "employees" : [ {"firstName" : "John", "lastName" : "Doe" }, {"firstName" : "Anna", "lastName" : "Smith" }, {"firstName" : "Peter", "lastName" : "Jones" } ]}; Display JSON Object: document.write(" ", json_obj.employees[0].firstName, " ", json_obj.employees[0].lastName, " "); h_array.html h_array.html

JSON uses JavaScript Syntax Because JSON uses JavaScript syntax, no 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"} ]; The first entry in the JavaScript object array can be accessed like this: employees[0].firstName + " " + employees[0].lastName; // returns John Doe It can also be accessed like this: employees[0]["firstName"] + " " + employees[0]["lastName"]; // returns John Doe Data can be modified like this: employees[0].firstName = "Gilbert"; It can also be modified like this: employees[0]["firstName"] = "Gilbert";

JSON Files The file type for JSON files is ".json" The MIME type for JSON text is "application/json" A common use of JSON is to read data from a web server, and display the data in a web page. For simplicity, this can be demonstrated by using a string as input (instead of a file).

JSON Example – Converts JSON text to Object Create a JavaScript string containing JSON syntax: var text = '{ "employees" : [' + '{ "firstName":"John", "lastName":"Doe" },' + '{ "firstName":"Anna", "lastName":"Smith" },' + '{ "firstName":"Peter", "lastName":"Jones" } ]}'; JSON syntax is a subset of JavaScript syntax. The JavaScript function JSON.parse(text) can be used to convert a JSON text into a JavaScript object: var obj = JSON.parse(text); Use the new JavaScript object in your page: document.getElementById("demo").innerHTML = obj.employees[1].firstName + " " + obj.employees[1].lastName;