CSC 337. JavaScript Object Notation (JSON) Rick Mercer

Similar documents
So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.

CSC Web Technologies, Spring Web Data Exchange Formats

Working with Database. Client-server sides AJAX JSON Data formats Working with JSON data Request Response Bytes Database

This tutorial will help you understand JSON and its use within various programming languages such as PHP, PERL, Python, Ruby, Java, etc.

REST. Web-based APIs

16. Objects & JSON. Dr. Dave Parker. Informa;on and the Web, 2014/15


Introduction to JSON. Roger Lacroix MQ Technical Conference v

What is Java Script? Writing to The HTML Document. What Can JavaScript do? CMPT 165: Java Script

JavaScript Lecture 1

Kyle Rainville Littleton Coin Company

Asynchronous JavaScript + XML (Ajax)

JSON is a light-weight alternative to XML for data-interchange JSON = JavaScript Object Notation

/ Introduction to XML

AJAX. Ajax: Asynchronous JavaScript and XML *

[301] JSON. Tyler Caraza-Harter

The Web: Concepts and Technology

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

10.1 Overview of Ajax

JSON. Dr. Kanda Runapongsa Saikaew Computer Engineering Department

AJAX: The Basics CISC 282 November 22, 2017

JavaScript. Like PHP, JavaScript is a modern programming language that is derived from the syntax at C.

JSON as an XML Alternative. JSON is a light-weight alternative to XML for datainterchange

Lecture 8 (7.5?): Javascript

Assigns a number to 110,000 letters/glyphs U+0041 is an A U+0062 is an a. U+00A9 is a copyright symbol U+0F03 is an

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

Grading for Assignment #1

Session 14. Serialization/JSON. Lecture Objectives

CGS 3066: Spring 2015 JavaScript Reference

CSC 309 The Big Picture

Welcome to CS50 section! This is Week 10 :(

TIME SCHEDULE MODULE TOPICS PERIODS. HTML Document Object Model (DOM) and javascript Object Notation (JSON)

JavaScript CS 4640 Programming Languages for Web Applications

User Interaction: XML and JSON

PVB CONTACT FORM 7 CALCULATOR PRO DOCUMENTATION

XML JavaScript Object Notation JSON Cookies Miscellaneous What Javascript can t do. OOP Concepts of JS

JavaScript CS 4640 Programming Languages for Web Applications

2/6/2012. Rich Internet Applications. What is Ajax? Defining AJAX. Asynchronous JavaScript and XML Term coined in 2005 by Jesse James Garrett

<form>. input elements. </form>

Arrays. Array Basics. Chapter 8 Spring 2017, CSUS. Chapter 8.1

Introduction to Python

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

XML Processing & Web Services. Husni Husni.trunojoyo.ac.id

Client vs Server Scripting

Dynamic Select Option Menu Using Ajax And PHP. Wednesday, Mar 11, 2015

Extensible Markup Language (XML) What is XML? Structure of an XML document. CSE 190 M (Web Programming), Spring 2007 University of Washington

1/22/2017. Chapter 2. Functions and Control Structures. Calling Functions. Objectives. Defining Functions (continued) Defining Functions

DC71 INTERNET APPLICATIONS JUNE 2013

Jquery Manually Set Checkbox Checked Or Not

Introduction to AJAX Bringing Interactivity & Intuitiveness Into Web Applications. By : Bhanwar Gupta SD-Team-Member Jsoft Solutions

AJAX ASYNCHRONOUS JAVASCRIPT AND XML. Laura Farinetti - DAUIN

A.A. 2008/09. What is Ajax?

Chapter 9: Simple JavaScript

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Week 13 Thursday (with Page 5 corrections)

AJAX: Introduction CISC 282 November 27, 2018

ACCESSING REMOTE DATABASE IN IOS APPLICATION USING JSON PARSING WITH OBJECTIVE-C

Jquery Ajax Json Php Mysql Data Entry Example

C R E A T I V E A C A D E M Y. Web Development. Photography

Working with Javascript Building Responsive Library apps

External data representation

JavaScript s role on the Web

CS312: Programming Languages. Lecture 21: JavaScript

Packaging Data for the Web

JSON Support for Junos OS

AIM. 10 September

Session 18. jquery - Ajax. Reference. Tutorials. jquery Methods. Session 18 jquery and Ajax 10/31/ Robert Kelly,

Decision Making in C

Cleveland State University Department of Electrical and Computer Engineering. CIS 408: Internet Computing

Human-Computer Interaction Design

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

AJAX and JSON. Day 8

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Creating an Online Catalogue Search for CD Collection with AJAX, XML, and PHP Using a Relational Database Server on WAMP/LAMP Server

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

JavaScript CSCI 201 Principles of Software Development

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

jquery and AJAX

LAST WEEK ON IO LAB. Install Firebug and Greasemonkey. Complete the online skills assessment. Join the mailing list.

Project 3 Web Security Part 1. Outline

ITS331 Information Technology I Laboratory

Assignment #3 CSCI 201 Spring % of course grade Title Weathermeister Back-End API Integration

Controlled Assessment Task. Question 1 - Describe how this HTML code produces the form displayed in the browser.

IBM Informix xC2 Enhancements IBM Corporation

Assignment: Seminole Movie Connection

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

JavaScript Programming

University of Washington, CSE 154 Homework Assignment 8: Baby Names

COMP519 Web Programming Lecture 27: PHP (Part 3) Handouts

COMP9321 Web Application Engineering

Contents. Demos folder: Demos\14-Ajax. 1. Overview of Ajax. 2. Using Ajax directly. 3. jquery and Ajax. 4. Consuming RESTful services

Introduction to Azure DocumentDB. Jeff Renz, BI Architect RevGen Partners

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer

Networking & The Web. HCID 520 User Interface Software & Technology

grabattention The jquery Plug-in

Notes General. IS 651: Distributed Systems 1

Iteration and Arrays Dr. Abdallah Mohamed

10/18/2017. Announcements. NoSQL Motivation. NoSQL. Serverless Architecture. What is the Problem? Database Systems CSE 414

PES DEGREE COLLEGE BANGALORE SOUTH CAMPUS 1 K.M. before Electronic City, Bangalore WEB PROGRAMMING Solution Set II

Transcription:

CSC 337 JavaScript Object Notation (JSON) Rick Mercer

Why JSON over XML? JSON was built to know JS

JSON JavaScript Object Notation Data-interchange format Lightweight Replacement for XML It's just a string with some special formatting Based on JavaScript but JSON has nothing to do with JavaScript

Characteristics Language Independent Again, just a big string with special chars like {, ' " : [ ] Text-based Light-weight Easy to encode inside a <?php block PHP has a method for this: json_encode($array); Easy to parse inside a <script> of.js file JS method: var array = JSON.parse(xhttp.responseText);

JSON is not JSON is not a style like CSS JSON is not a markup language like HTML JSON is not a programming language like JavaScript or PHP JSON is not a general serialization format as seen in Java

JSON Values value st ring num ber objec t array true false null

Literal Values in JSON number: 1-2 4.56 string: "need double quotes" object: { "name":"dakota", "age": 20 null: null boolean: true false arrays: [1, 2, 3] arrays of objects { "students" : [ {"name":"li", "age": 21, {"name":"ty", "age": 19 ]

Important usage! Send a JSON string from server to client. Needed: JSON.parse <?php echo '{ "name":"dakota", "age":21 ';?> <script>... ajax.onreadystatechange = function() { if (ajax.readystate == 4 && ajax.status == 200) {... </script> var student = JSON.parse(ajax.responseText); document.write(student.name + '<br>'); document.write(student.age + '<br>');

JSON Arrays Square brackets hold arrays (JSON has no indexed arrays) [1, 2, 3 ] JSON array are very much like JavaScript arrays JSON arrays can't store as many different types as JS array

<?php $array = [123, 'a string', true && false]; echo json_encode($array);?> <input type="button" value="click me" onclick="afunction()"> <br> <div id="tochange"> Should see an array here on click </div> <script> function afunction() { var ajax = new XMLHttpRequest(); ajax.open("get", "array.php", true); ajax.send(); ajax.onreadystatechange = function () { if (ajax.readystate == 4 && ajax.status == 200) { var array = JSON.parse(ajax.responseText); for(i = 0; i < array.length; i++) document.write(array[i] + "<br>"); ; // End anonymous function

Code demo Write a service that send back an array of string or and array of integers depending Need isset and $_GET[]

JSON Objects A JSON object is an unordered set of name/value pairs A colon exist between the name and the value Data is separated by commas Curly braces hold objects { "name": "Chris Miller", "age":20, "year": "Sophomore"

JSON objects look like JavaScript objects <script> var array = [1,2,3]; for (i = 0; i < 3; i++ ) document.write(array[i] + " "); document.write("<br>"); var string = '[1,2,3]'; // Convert a JSON string to JavaScript var JSarr = JSON.parse(string); for (i = 0; i < 3; i++ ) document.write(jsarr[i] + " "); </script>

Array of objects; the same code in JS and JSON <script> // Strings like this will be echoed from a PHP script on the server var string = '{ "student": [ ' + '{"name": "Chris", "age":20,' + '{"name": "Devon", "age":19' + '] ' var JSref = JSON.parse(string); for (i = 0; i < 3; i++ ) { document.write(jsref.student[i].name + " is " + JSref.student[i].age ); document.write("<br>"); </script>

JSON data converted to JS to change the DOM <h2>create Object from JSON String</h2> <p id="demo"></p> <script> // Store an array of objects: var JSON_arrayOfObjects = '{"accounts":[' + '{"ID":"Casey", "pin":"1234",' + '{"ID":"Devon", "pin":"5678",' + '{"ID":"Lindsay", "pin":"9999"' + ']' + ''; ref = JSON.parse(JSON_arrayOfObjects); document.getelementbyid("demo").innerhtml = ref.accounts[1].id + " has a pin of " + ref.accounts[1].pin; </script>

Javascript objects look like JSON objects One way to create a JavaScript Object, use { // Store an JS object with a JS object inside it: var JSObject = { "id":'s123456', "inner": { "CSC127A": 'MWF 10-10:50', "CSC337": 'MWF 2:00-2:50', "CSC335": 'MWF 10-10:50' ; document.write(jsobject.id + '<br>'); document.write(jsobject.inner.csc127a + '<br>'); document.write(jsobject.inner.csc337 + '<br>'); document.write(jsobject.inner.csc335 + '<br>');

JSON Arrays Arrays are ordered sequences of values Arrays are wrapped in [ ] separates values with, JSON does not have indexed arrays

JSON arrays in PHP as a string sent to AJAX <?php echo '["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]';?> ajax.onreadystatechange = function() { if (ajax.readystate == 4 && ajax.status == 200) { var array = JSON.parse(ajax.responseText); for(index = 0; index < array.length; index++) { document.write(array[index] + '<br>');

json_encode When exchanging data between a browser and a server, the data can only be echoed as text, JSON for example. We will use json_encode <?php $array = [123, 'a string', true && false]; echo json_encode($array);?> We convert any JSON received from the server into JS objects ajax.onreadystatechange = function() { if (ajax.readystate == 4 && ajax.status == 200) { var array = JSON.parse(ajax.responseText); document.write('<b>' + array[0] + '</b>' + '<br>'); document.write(array[1] + '<br>'); document.write('<i>' + array[2] + '</i>');