JSON (JavaScript Object Notation).  A lightweight data-interchange format  A subset of the object literal notation of JavaScript (or ECMA-262).  A.

Slides:



Advertisements
Similar presentations
Copyright © Steven W. Johnson
Advertisements

REST and JSON in WCF It is possible to implement and access REST services in WCF And still doing it object oriented This is a short presentation on how.
JSON Valery Ivanov.
9. AJAX & RIA. 2 Motto: O! call back yesterday, bid time return. — William Shakespeare.
PHP Intro/Overview Squirrel Book pages Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a.
XMLHttpRequest Object and XML What we should learn in this lesson –What is the XHR object? –How to create the XHR objects? –XHR object properties –XHR.
15-Jul-15 JSON. JSON example “JSON” stands for “JavaScript Object Notation” Despite the name, JSON is a (mostly) language-independent way of specifying.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
BUILDING A FACEBOOK APP. STEP 1 Create a Developers License. Make sure to take note/record the app id/key.
Databases.  Multi-table queries  Join ▪ An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them. 
CS378 - Mobile Computing Web - WebView and Web Services.
Lecture 12 – AJAX SFDV3011 – Advanced Web Development Reference: 1.
JSON The Fat Free Alternative to XML. Data Interchange The key idea in Ajax. An alternative to page replacement. Applications delivered as pages. How.
RESTful applications Norman White. REST Representational state transfer Key concepts – Client Server architecture built on transferring resources between.
Lecture 13 – XML and JSON SFDV3011 – Advanced Web Development Reference: 1.
WEB API: WHY THEY MATTER ECOL 453/ Nirav Merchant
HTML5 Communication. The Setup Somewhere on the web, a server makes a ”service” available, that we wish to use in a web application The service may offer.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Cross Site Integration “mashups” cross site scripting.
AJAX (also known as: XMLHTTP, Remote Scripting, XMLHttpRequest, etc.) Matt Warden.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
JSON Java Script Object Notation Copyright © 2013 Curt Hill.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 16: PHP Types.
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.
JSON and A Comparison of Scripts. JSON: JavaScript Object Notation Based on a subset of the JavaScript Programming Language provides a standardized data.
Dimiter Kunchev.  JavaScript library written by Sam Stephenson   Adds object oriented techniques  Provides.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Asynchronous Javascript And XML AJAX : an introduction UFCEUS-20-2 : Web Programming.
School of Computing and Information Systems CS 371 Web Application Programming AJAX.
 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.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
AJAX Asynchronous JavaScript and XML 1. AJAX Outline What is AJAX? Benefits Real world examples How it works 2.
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.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
AJAX and REST. Slide 2 What is AJAX? It’s an acronym for Asynchronous JavaScript and XML Although requests need not be asynchronous It’s not really a.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
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.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JSON JavaScript Object Notation. Introduction (What is JSON?) A lightweight text based data-interchange format Language independent Subset of JavaScript.
AJAX CS456 Fall Examples Where is AJAX used? Why do we care?
1 AJAX. AJAX – Whatzit? Asynchronous (content loading)‏ Javascript (logic & control)‏ And XML (request handling)‏
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, Sixth Edition Chapter 11 Updating Web Pages with Ajax.
JSON (Copied from and from Prof Da Silva) Week 12 Web site:
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
JSON JavaScript Object Notation Douglas Crockford Yahoo! Inc.
Web Services Essentials. What is a web service? web service: software functionality that can be invoked through the internet using common protocols like.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
JavaScript 4 JavaScript 4 Objects and JSON Dr Kevin McManus with material borrowed from Gill Windall
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
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.
PHP LANGUAGE MULTIPLE CHOICE QUESTION SET-5
Unit 4 Representing Web Data: XML
AJAX and REST.
JSON Crash Course Traversy Media.
AJAX.
Chapter 7 Representing Web Data: XML
Built in Fairfield County: Front End Developers Meetup
Asynchronous Javascript And XML
JavaScript & jQuery AJAX.
JSON with PHP Week 11 – 13.
JSON++ - A Simple class library for JSON
Department of Computer Science Cal State East Bay, Hayward, CA
PHP and JSON Topics Review JSON.
Presentation transcript:

JSON (JavaScript Object Notation)

 A lightweight data-interchange format  A subset of the object literal notation of JavaScript (or ECMA-262).  A JSON string must be enclosed by double quotes.  See for the detailed syntax of JSON.

JSON is built on two structures  A collection of name/value pairs.  In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.  e.g.: An object with three properties named "a", "b", and "c" { "a":1,"b":2,"c":3 }  An ordered list of values.  In most languages, this is realized as an array, vector, list, or sequence.  e.g.: An array of three integers and one string value [ 1, 2, 3, "value #4 with" ]

Using JSON in JavaScript  Need a JSON parser or a function, stringify(), to convert between JavaScript objects and JSON encoded data.   JSON encoded data  JavaScript object  var myObject = eval('(' + myJSONtext + ')');  var myObject = JSON.parse(myJSONtext);  JavaScript value  JSON encoded data  var myJSONText = JSON.stringify(myObject);

Using JSON with XmlHttpRequest  Sending JSON encoded data to the server  Use HTTP POST method and send the JSON encoded data in the body of the request // xmlhttp is an XmlHttpRequest object xmlhttp.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded;charset=UTF-8;' ); xmlhttp.send('jsondata=' + escape(myJSONText));  Handling JSON encoded data from the server  Server should set the content type to "text/plain"  In the handler function of xmlhttp object, read xmlhttp.responseText

Speeding Up AJAX with JSON  Both XML and JSON use structured approaches to mark up data.  More and more web services are supporting JSON  e.g.: Yahoo's various search services, travel planners, del.icio.us, and highway traffic services

Sean Kelly SK Consulting Main St Springfield, TX Main St Springfield, TX Example: An address book data encoded in XML

{ "fullname": "Sean Kelly", "org": "SK Consulting", " addrs": [ {"type": "work", "value": {"type": "home", "pref": 1, "value": ], "telephones": [ {"type": "work", "pref": 1, "value": " "}, {"type": "fax", "value": " "}, {"type": "mobile", "value": " "} ], "addresses": [ {"type": "work", "format": "us", "value": "1234 Main StnSpringfield, TX "}, {"type": "home", "format": "us", "value": "5678 Main StnSpringfield, TX "} ], "urls": [ {"type": "work", "value": " {"type": "home", "value": " ] } Example: The same address book data encoded in JSON

function myHandler() { if (req.readyState == 4 /*complete*/) { var addrField = document.getElementById('addr'); var root = req.responseXML; var addrsElem = root.getElementsByTagName('addresses')[0]; var firstAddr = addrsElem.getElementsByTagName('address')[0]; var addrText = fistAddr.firstChild; var addrValue = addrText.nodeValue; addrField.value = addrValue; } JavaScript code to handle XML encoded data function myHandler() { if (req.readyState == 4 /*complete*/) { var addrField = document.getElementById('addr'); var card = eval('(' + req.responseText + ')'); addrField.value = card.addresses[0].value; } JavaScript code to handle JSON encoded data Both examples try to update the value of a form element named "addr" with the data obtained from an HTTP request.

XML vs. JSON (in AJAX Application)  JSON produces slightly smaller documents  JSON is easier to use in JavaScript  Parsing JSON encoded data is much faster than parsing XML encoded data

XML vs. JSON (in AJAX Application)  Most web services provide only XML encoded data.  Your server-side script that serves as a proxy to external web services can convert XML-encoded data to JSON format.  Using eval() to parse JSON can be dangerous if the data are coming from an external source.  Alternatives – use a JSON parser  json.org provides a parser written in JavaScript  Some browsers support native JSON parser

Support for JSON in PHP  Bundled into PHP by default  JSON functions  json_decode — Decodes a JSON string json_decode  json_encode — Returns the JSON representation of a value json_encode  json_last_error — Returns the last error occured json_last_error

json_decode() mixed json_decode ( string $json, bool $assoc)  Takes a JSON encoded string and converts it into a PHP value.  $json  The JSON string being decoded  $assoc  false (default)  return the value as an object  true  return the value as an associative array

<?php $json = '{"a":1,"b":2,"c":3}'; var_dump(json_decode($json)); var_dump( json_decode($json, true) ); ?> <?php $json = '{"foo-bar": 12345}'; $obj = json_decode($json); print $obj->{'foo-bar'}; // ?> object(stdClass)#1 (3) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) } array(3) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) } json_decode: Example #1 json_decode: Example #2

<?php // the following strings are valid JavaScript but not valid JSON // the name and value must be enclosed in double quotes // single quotes are not valid $bad_json = "{ 'bar': 'baz' }"; json_decode($bad_json); // null // the name must be enclosed in double quotes $bad_json = '{ bar: "baz" }'; json_decode($bad_json); // null // trailing commas are not allowed $bad_json = '{ bar: "baz", }'; json_decode($bad_json); // null ?> json_decode: Example #3

json_encode() string json_encode ( mixed $value )mixed  Returns a string containing the JSON representation of $value.  $value  The value being encoded. Can be any type except a resource.  This function only works with UTF-8 encoded data.

<?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr); // Output {"a":1,"b":2,"c":3,"d":4,"e":5} $arr = array ( 1, 2, 3, 4, 5 ); echo json_encode($arr); // Output [1,2,3,4,5] $arr['x'] = 10; echo json_encode($arr); // Output {"0":1,"1":2,"2":3,"3":4,"4":5,"x":10} echo json_encode(54321); // Output ?> json_encode: Example #1

References  JSON   PHP Manual: JavaScript Object Notation   Speeding Up AJAX with JSON 