CSE 154 Lecture 13: XML and JSON.

Slides:



Advertisements
Similar presentations
Copyright © Steven W. Johnson
Advertisements

Sue Wills July Objects The JavaScript language is completely centered around objects, and because of this, it is known as an Object Oriented Programming.
The Web Warrior Guide to Web Design Technologies
CSE 190: Internet E-Commerce Lecture 17: XML, XSL.
Tutorial 10 Programming with JavaScript
XHTML and CSS Overview. Hypertext Markup Language A set of markup tags and associated syntax rules Unlike a programming language, you cannot describe.
CSE 154 LECTURE 23: XML. Storing structured data in arbitrary text formats (bad) My note: BEGIN FROM: Alice Smith TO: Robert Jones.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
CSE 154 LECTURE 24: XML AND JSON. Debugging responseXML in Firebug can examine the entire XML document, its node/tree structure.
Pemrograman Berbasis WEB XML part 2 -Aurelio Rahmadian- Sumber: w3cschools.com.
Javascript and the Web Whys and Hows of Javascript.
Creating a Basic Web Page
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
CS134 Web Design & Development Creating a Basic Web Page Mehmud Abliz.
JavaScript Programming B.Ramamurthy 6/113/2014B. Ramamurthy CSE6511.
Lecture 13 – XML and JSON SFDV3011 – Advanced Web Development Reference: 1.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
1 Midterm Review. 2 Midterm Exam  30% of your grade for the course  October14 at the regular class time  No makeup exam or alternate times  Closed.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
ITBP 119 Algorithms and Problem Solving Section 2.1 Installing software Section 2.2 First Programs Section 2.3 Variables.
Copyright (c) 2004 Prentice-Hall. All rights reserved. 1 Committed to Shaping the Next Generation of IT Experts. Creating XHTML Documents Essentials for.
Chapter 2 Web Page Design Mr. Gironda. Elements of a Web Page These are things that most web pages use.
HTML Lesson 3 Hyper Text Markup Language. Assignment Part 2  Set the file name as “FirstName2.htm”  Set the title as “FirstName LastName First Web Site”
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
1 Final Review. 2 Final Exam  30% of your grade for the course  December 9 at 7:00 p.m., the regular class time  No makeup exam or alternate times.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
HTML Basics. HTML Coding HTML Hypertext markup language The code used to create web pages.
AJAX. Ajax  $.get  $.post  $.getJSON  $.ajax  json and xml  Looping over data results, success and error callbacks.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
JQUERY AND AJAX
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JSON (Copied from and from Prof Da Silva) Week 12 Web site:
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
XML & JSON. Background XML and JSON are to standard, textual data formats for representing arbitrary data – XML stands for “eXtensible Markup Language”
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
CSE 154 LECTURE 13: XML AND JSON. Debugging responseXML in Firebug can examine the entire XML document, its node/tree structure.
Introduction to.
Build in Objects In JavaScript, almost "everything" is an object.
HTML Basics.
CSE 154 Lecture 12: XML.
Programming Web Pages with JavaScript
“Under the hood”: Angry Birds Maze
CSE 154 Lecture 12: XML.
Web Development & Design Foundations with HTML5
JavaScript is a programming language designed for Web pages.
© 2015, Mike Murach & Associates, Inc.
CSE 154 Lecture 24: JSON.
Prepared for Md. Zakir Hossain Lecturer, CSE, DUET Prepared by Miton Chandra Datta
JavaScript an introduction.
Web Systems Development (CSC-215)
Tutorial 10 Programming with JavaScript
CS 240 – Advanced Programming Concepts
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
Lecture 14: JSON and Web SERVICES
CSc 337 Lecture 13: JSON.
Introduction to AJAX and JSON
CSc 337 Lecture 13: JSON.
Presentation transcript:

CSE 154 Lecture 13: XML and JSON

Schemas and Doctypes "rule books" describing which tags/attributes you want to allow in your data used to validate XML files to make sure they follow the rules of that "flavor" the W3C HTML validator uses an HTML schema to validate your HTML (related to <!DOCTYPE html> tag) these are optional; if you don't have one, there are no rules beyond having well- formed XML syntax for more info: W3C XML Schema Document Type Definition (DTD) ("doctype")

Exercise: Late day distribution Write a program that shows how many students turn homework in late for each assignment. Data service here: http://webster.cs.washington.edu/cse154/services/hw/hw.php parameter: assignment=hwN

An example of XML data fairly simple to read and understand <?xml version="1.0" encoding="UTF-8"?> <note private="true"> <from>Alice Smith (alice@example.com)</from> <to>Robert Jones (roberto@example.com)</to> <to>Charles Dodd (cdodd@example.com)</to> <subject>Tomorrow's "Birthday Bash" event!</subject> <message language="english"> Hey guys, don't forget to call me this weekend! </message> </note> XML fairly simple to read and understand can be parsed by JavaScript code using XML DOM Is there any other data format that is more natural for JS code to process?

JavaScript Object Notation (JSON) JavaScript Object Notation (JSON): Data format that represents data as a set of JavaScript objects invented by JS guru Douglas Crockford of Yahoo! natively supported by all modern browsers (and libraries to support it in old ones) not yet as popular as XML, but steadily rising due to its simplicity and ease of use

Background: Creating a new object var name = { fieldName: value, ... fieldName: value }; JS var pt = { x: 4, y: 3 }; pt.z = -1; alert("(" + pt.x + ", " + pt.y + ", " + pt.z + ")"); // (4, 3, -1) in JavaScript, you can create a new object without creating a class you can add properties to any object even after it is created (z)

More about JavaScript object syntax var person = { name: "Philip J. Fry", // string age: 23, // number "weight": 172.5, // number friends: ["Farnsworth", "Hermes", "Zoidberg"], // array getBeloved: function() { return this.name + " loves Leela"; } }; alert(person.age); // 23 alert(person["weight"]); // 172.5 alert(person.friends[2])); // Zoidberg alert(person.getBeloved()); // Philip J. Fry loves Leela an object can have methods (function properties) that refer to itself as this can refer to the fields with .fieldName or ["fieldName"] syntax field names can optionally be put in quotes (e.g. weight above)

Repeated: Example XML data <?xml version="1.0" encoding="UTF-8"?> <note private="true"> <from>Alice Smith (alice@example.com)</from> <to>Robert Jones (roberto@example.com)</to> <to>Charles Dodd (cdodd@example.com)</to> <subject>Tomorrow's "Birthday Bash" event!</subject> <message language="english"> Hey guys, don't forget to call me this weekend! </message> </note> XML Could we express this message data as a JavaScript object? Each attribute and tag could become a property or sub-object within the overall message object

The equivalant JSON data { "private": "true", "from": "Alice Smith (alice@example.com)", "to": [ "Robert Jones (roberto@example.com)", "Charles Dodd (cdodd@example.com)" ], "subject": "Tomorrow's \"Birthday Bash\" event!", "message": { "language": "english", "text": "Hey guys, don't forget to call me this weekend!" } } JSON

Valid JSON JSON has a few rules that differ from regular JS: var student = { // no variable assignment "first_name": 'Bart', // strings must be double-quoted last_name: "Simpson", // property names must be quoted "birthdate": new Date("April 1, 1983"), // Date objects not supported "enroll": function() { // Functions not supported this.enrolled = true; } }; JSON JSON has a few rules that differ from regular JS: Strings must be quoted (in JS, single- or double-quoted are allowed) All property/field names must be quoted Unsupported types: Function, Date, RegExp, Error All others supported: Number, String, Boolean, Array, Object, null Numerous validators/formatters available: JSONLint, JSON Formatter & Validator, Free Formatter, JSON Validator

Browser JSON methods method description JSON.parse(string) converts the given string of JSON data into an equivalent JavaScript object and returns it JSON.stringify(object) converts the given object into a string of JSON data (the opposite of JSON.parse) you can use Ajax to fetch data that is in JSON format then call JSON.parse on it to convert it into an object then interact with that object as you would with any other JavaScript object

JSON expressions exercise Given the JSON data at right, what expressions would produce: The window's title? (use the Console) The image's third coordinate? The number of messages? The y-offset of the last message? var data = JSON.parse(this.responseText); { "window": { "title": "Sample Widget", "width": 500, "height": 500 }, "image": { "src": "images/logo.png", "coords": [250, 150, 350, 400], "alignment": "center" "messages": [ {"text": "Save", "offset": [10, 20]}, {"text": "Help", "offset": [ 0, 50]}, {"text": "Quit", "offset": [30, 15]} ], "debug": "true" } JSON var title = data.window.title; var coord = data.image.coords[2]; var len = data.messages.length; var y = data.messages[len - 1].offset[1];

JSON example: Books Suppose we have a service books_json.php about library books. If no query parameters are passed, it outputs a list of book categories: { "categories": ["computers", "cooking", "finance", ...] } JSON Supply a category query parameter to see all books in one category:  http://webster.cs.washington.edu/services/books/books_json.php?category=cooking { "books": [ {"category": "cooking", "year": 2009, "price": 22.00, "title": "Breakfast for Dinner", "author": "Amanda Camp"}, {"category": "cooking", "year": 2010, "price": 75.00, "title": "21 Burgers for the 21st Century", "author": "Stuart Reges"}, ... ] } JSON

JSON exercise Books in category "Cooking": Write a page that processes this JSON book data. Initially the page lets the user choose a category, created from the JSON data. After choosing a category, the list of books in it appears: Books in category "Cooking": Breakfast for Dinner, by Amanda Camp (2009) 21 Burgers for the 21st Century, by Stuart Reges (2010) The Four Food Groups of Chocolate, by Victoria Kirst (2005)

Bad style: the eval function // var data = JSON.parse(this.responseText); var data = eval(this.responseText); // don't do this! ... JS JavaScript includes an eval keyword that takes a string and runs it as code this is essentially the same as what JSON.parse does, but JSON.parse filters out potentially dangerous code; eval doesn't eval is evil and should not be used!