CSE 154 Lecture 24: JSON.

Slides:



Advertisements
Similar presentations
JavaScript and AJAX Jonathan Foss University of Warwick
Advertisements

Adding Dynamic Content to your Web Site
CSE 190: Internet E-Commerce Lecture 17: XML, XSL.
The Application Layer Chapter 7. Electronic Mail Architecture and Services The User Agent Message Formats Message Transfer Final Delivery.
Multiple Tiers in Action
CSE 154 LECTURE 23: XML. Storing structured data in arbitrary text formats (bad) My note: BEGIN FROM: Alice Smith TO: Robert Jones.
More APIs: Web Services CMPT 281. Announcements Project milestone Lab: – Web services examples.
CSE 154 LECTURE 9: FORMS. Web data most interesting web pages revolve around data examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes can.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Date: May 5 th,2012.
CMPT Web Programming Introduction and Basic HTML.
Computer Concepts 2014 Chapter 7 The Web and .
CSE 154 LECTURE 24: XML AND JSON. Debugging responseXML in Firebug can examine the entire XML document, its node/tree structure.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Web Application Architecture and Communication. Displaying a Web page in a Browser
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
CSCI 6962: Server-side Design and Programming Web Services.
Cross Site Integration “mashups” cross site scripting.
CSE 154 LECTURE 5: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Operating Systems Lesson 12. HTTP vs HTML HTML: hypertext markup language ◦ Definitions of tags that are added to Web documents to control their appearance.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
Internet Applications (Cont’d) Basic Internet Applications – World Wide Web (WWW) Browser Architecture Static Documents Dynamic Documents Active Documents.
ASP. ASP is a powerful tool for making dynamic and interactive Web pages An ASP file can contain text, HTML tags and scripts. Scripts in an ASP file are.
AJAX. Ajax  $.get  $.post  $.getJSON  $.ajax  json and xml  Looping over data results, success and error callbacks.
INTRODUCTION JavaScript can make websites more interactive, interesting, and user-friendly.
CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
 Before you continue you should have a basic understanding of the following:  HTML  CSS  JavaScript.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
COSC 2328 – Web Programming.  PHP is a server scripting language  It’s widely-used and free  It’s an alternative to Microsoft’s ASP and Ruby  PHP.
CSE 154 LECTURE 17: WEB SERVICES. What is a web service? web service: software functionality that can be invoked through the internet using common protocols.
Basic Web Page Design. Text book: HTML, XHTML, and CSS: Visual QuickStart Guide, Sixth Edition written by Elizabeth Castro. Software: Adobe® Dreamweaver®
Web Services Essentials. What is a web service? web service: software functionality that can be invoked through the internet using common protocols like.
CSE 154 LECTURE 18: FORMS AND UPLOADING FILES. Exercise: Baby name web service JSON Modify our babynames.php service to produce its output as JSON. For.
CITA 330 Section 10 Web Remoting Techniques. Web Remoting Web Remoting is a term used to categorize the technique of using JavaScript to directly make.
CSE 154 LECTURE 18: WEB SERVICES. Exercise: Baby name web service Write a web service that accepts a name and gender and finds and outputs the line from.
National College of Science & Information Technology.
REEM ALMOTIRI Information Technology Department Majmaah University.
CSE 154 LECTURE 13: XML AND JSON. Debugging responseXML in Firebug can examine the entire XML document, its node/tree structure.
PHP using MySQL Database for Web Development (part II)
Internet/Web Databases
Lecture 16: File I/O; Functions
Web Basics: HTML/CSS/JavaScript What are they?
CSE 154 Lecture 13: XML and JSON.
CSE 154 Lecture 11: AJAx.
CSE 154 Lecture 12: XML.
Introduction to ASP By “FlyingBono” 2009_01 By FlyingBono 2009_01
Chapter 2 Client/Server Applications
Lecture 11. Web Standards Continued
PHP Introduction.
Tutorial (4): HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Prepared for Md. Zakir Hossain Lecturer, CSE, DUET Prepared by Miton Chandra Datta
Lecture 19: Forms and uploading files
CSE 154 Lecture 11: AJAx.
CSE 154 Lecture 22: AJAX.
Asynchronous Javascript And XML
JavaScript & jQuery AJAX.
Unit 6 part 3 Test Javascript Test.
Lecture 17: Web Service and post
Lecture 16: Writing Your Own Web Service
Introduction and Basic HTML
Lecture 14: JSON and Web SERVICES
CSc 337 Lecture 13: JSON.
CSE 154 Lecture 15: MORE PHP.
Client-Server Model: Requesting a Web Page
CSc 337 Lecture 13: JSON.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Lecture 15: Writing Your Own Web Service
Presentation transcript:

CSE 154 Lecture 24: JSON

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

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 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!

What is a web service? web service: software functionality that can be invoked through the internet using common protocols like a remote function(s) you can call by contacting a program on a web server many web services accept parameters and produce results can be written in PHP and contacted by the browser in HTML and/or Ajax code service's output might be HTML but could be text, XML, JSON or other content examples seen in CSE 154: quote.php, animalgame.php, books_json.php, urban.php,  weather.php

Setting content type with header header("Content-type: type/subtype"); PHP header("Content-type: text/plain"); print "This output will appear as plain text now!\n"; PHP by default, a PHP file's output is assumed to be HTML (text/html) use the header function to specify non-HTML output must appear before any other output generated by the script

Recall: Content ("MIME") types related file extension text/plain .txt text/html .html, .htm, ... text/xml .xml application/json .json text/css .css text/javascript .js image/gif .gif Lists of MIME types: by type, by extension

Example: Exponent web service Write a web service that accepts a base and exponent and outputs base raised to the exponent power. For example, the following query should output 81 : http://example.com/exponent.php?base=3&exponent=4 solution: <?php header("Content-type: text/plain"); $base = (int) $_GET["base"]; $exp = (int) $_GET["exponent"]; $result = pow($base, $exp); print $result; ?> PHP