introduction to json and building youtube, twitter and wikipedia web apps

43
INTRODUCTION TO JSON JavaScript Object Notations R a g h a v e n d r a N a y a k M

Upload: raghavendra-nayak

Post on 08-May-2015

6.318 views

Category:

Technology


1 download

DESCRIPTION

JSON (JavaScript Object Notation) is a data interchange format. JSON is derived from Javascript but is platform independent. In this slide we shall concentrate on building web applications using JSON. This slide covers all the basics which are required to build JSON application.

TRANSCRIPT

Page 1: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

INTRODUCTION TO JSONJavaScript Object Notations

Raghavendra

Nayak M

Page 2: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

CREATED BY RAGHAVENDRA NAYAK M

All sample codes in this slide are released under Public Domain.

Contact me at http://twitter.com/MRNayak [email protected]

Raghavendra

Nayak M

Page 3: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

WHAT WE WILL LEARN TODAY?

Basics of HTML Basics of JavaScript JSON Building YouTube App Twitter App Wikipedia App

Raghavendra

Nayak M

Page 4: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

WHY JSON?

JSON is an alternative to XML Using JSON we can build various 3rd party

applications. It is easy to retrieve Tweets, YouTube Videos

and many more using JSON. Programming is simple and easy. JSON data is easy to read. JSON parsers are available in various

JavaScript Framework like jQuery, Yahoo UI

Raghavendra

Nayak M

Page 5: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

BASICS ABOUT HTML

HTML is a language for describing web pages.

It is a markup language and not a programming language.

Markup language is set of various tags. It uses markup language to describe a page. A web page with .php, .py, .rb is a

dynamically generated HTML page using backend languages.

Raghavendra

Nayak M

Page 6: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

SIMPLE HTML PAGE

<html><head><title>Hello Everyone</title><link rel="stylesheet" type="text/css" href="style.css" ></link></head>

<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body></html>

Raghavendra

Nayak M

Page 7: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

HTML HEADING TAG

<h1>-<h6> Heading tags in HTML are defined from <h1> to <h6>

<h1> Hello Everyone </h1> <h2> Hello Everyone </h1> …. … <h6> Hello Everyone </h6>

Raghavendra

Nayak M

Page 8: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

PARAGRAPH TAG

In HTML paragraphs are defined using <p> tag.

<p> Hello </p> <p> A quick brown fox jumped over a lazy

village dog.</p>

Raghavendra

Nayak M

Page 9: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

BREAK TAG

<br> tag in HTML adds a single line break. If we want content to appear in new line then

we must use break tag.

Raghavendra

Nayak M

Page 10: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

ANCHOR TAG

In HTML we use <a> tag (Anchor Tag) to create links

<a href=“http://www.google.com”>Google</a>

Here href contains the link to another document.

We can also call JavaScript functions using anchor tag.

<a href=“#” onclick=“smefunc();”> Click Me</a>

Raghavendra

Nayak M

Page 11: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

IMAGE TAG

We use <img> tag to embed images in a webpage.

<img src=“a.jpg”></img> Above tag will embed a image a.jpg. We can use images for creating links. <a href=“http://www.google.com” ><img

src=“a.png”/></a> We can specify height and width of image <img src=“a.jpg” width=“200px”

height=“100px” alt=“goog”/>

Raghavendra

Nayak M

Page 12: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

DIVISION TAG

A division tag or div tag (<div>) is used to create division in HTML

<div id=“one” class=“three”> id specify unique id for element. class specify class name for an element. There can be two or more elements with

same class name.

Raghavendra

Nayak M

Page 13: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

LINK TAG

Link Tag specifies link between existing document and external resources

<link rel="stylesheet" type="text/css" href=“style.css" />

We use link to specify external stylesheet.

Raghavendra

Nayak M

Page 14: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

SCRIPT TAG

Script tag is used to include client side scripts like JavaScript.

<script type=“text/javascript”>alert(‘Msg”);</script>

Script tag can also contain external scripts. <script type=“text/javascript”

src=“hme.js” />

Raghavendra

Nayak M

Page 15: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

BASICS OF JAVASCRIPT

JavaScript is a client side Scripting Language. It helps to add dynamic effect to web pages. JavaScript can be used to create image

sliders, charts, AJAX Applications and many more.

JavaScripts are used as event handlers. JavaScript are used to create and read

cookie. Various Javascript Frameworks like jQuery,

MooTools, YUI make programming even more easier.

Raghavendra

Nayak M

Page 16: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

VARIABLES IN JAVASCRIPT

In JavaScript we define Variables using ‘var’ <script type=“text/javascript”> var a=10;//Integer var b=‘Hello’;//Char alert(b);//a dialogbox with contents of

variable b // Single line comment /* Multiline Comment */ </script>

Raghavendra

Nayak M

Page 17: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

GETTING CLIENT SIDE DATE

Its is possible to get users date, browser, os etc using JavaScript

<script type=“text/javascript”> var d=new Date(); alert(d); </script>

Raghavendra

Nayak M

Page 18: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

PRINTING VARIABLES IN JS

Just like printf in C, In JavaScript we use document.write();

<script type=“text/javascript”> document.write(“Hello Everyone”); </script> <script type=“text/javascript”> var a=‘Hello World’; document.write(a); </script>

Raghavendra

Nayak M

Page 19: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

+ OPERATOR IN JAVASCRIPT

+ operator is used to add integers. + operator is used to concatenate to strings <script type=“text/javascript”> var a=2; var b=3; document.write(a+b); var c=‘Hello’; var d=‘everyone’; document.write(c+d); </script>

Raghavendra

Nayak M

Page 20: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

JS AND C

Syntax of if…else , for loop , while loop in JavaScript are same as that of C.

if(i==0) { for(j=0;j<n;j++) { //sme code } else alert(‘i !=0’);

Raghavendra

Nayak M

Page 21: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

ARRAYS IN JAVASCRIPT

Array is used to store multiple values in single variable.

<script type=“text/javascript”> var dept=new Array(4); dept[0]=‘CSE’; dept[1]=‘ISE’; dept[2]=‘ECE’; dept[4]=‘ME’; for(var i=0;i<dept.length;i++) document.write(dept[i]); </script>

Raghavendra

Nayak M

Page 22: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

PROBLEMS WITH ARRAY

It is difficult to handle large volume of data using Arrays.

This problem can be solved by using JSON

Raghavendra

Nayak M

Page 23: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

JSON

JSON is a human readable data interchange. It is derived from JavaScript, But it is

language independent. JSON supports strings, numbers, objects,

arrays and Boolean It is primarily used to send data from web

server to application. There are inbuilt functions and libraries to

parse JSON in many languages.

Raghavendra

Nayak M

Page 24: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

SIMPLE JSON FORMAT

{ "id": 1, "name": “Raghavendra", “dept": “CSE”, } JSON starts with ‘{‘ and ends with ‘}’ JSON has a optional callback myJSON({“id”:1,”name”:”Raghavendra”,”dep

t”:”CSE”});

Raghavendra

Nayak M

Page 25: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

SIMPLE C PROGRAM

#include<stdio.h> void p(char a[]) { printf(“%s”,a); } void main() { char ab[4]={‘H’,’E’,’L’,’L’,’O’}; p(ab); } When we run above program, it will print HELLO,

Similarly we build a json parser and then call parsing function with JSON as an argument.

Raghavendra

Nayak M

Page 26: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

PARSING JSON USING JS {"students": [ { "name":"Rahul", "usn":"cs066" }, { "name":"Rajiv", "usn":"cs068" }, { "name":"Rudresh", "usn":"cs072" }, ] }

Raghavendra

Nayak M

Page 27: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

JS CODE FOR PARSING

If we specify a callback to above student json and if call back url is MyJson then

MyJson({"students":[{"name":"Rahul","usn":"cs066"},{"name":"Rajiv","usn":"cs068"},{"name":"Rudresh","usn":"cs072"}]});

Raghavendra

Nayak M

Page 28: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

WRITING MYJSON FUNCTION

<script type=“text/javascript”> Function MyJson(data) { For(var i=0;i<data.student.length;i++) { var name=data.student[i].name; var usn=data.student[i].usn; document.write(usn+” “+name); } } </script>

Raghavendra

Nayak M

Page 29: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

COMPLETE CODE <script type=“text/javascript”> function MyJson(data) { For(var i=0;i<data.student.length;i++) { var name=data.student[i].name; var usn=data.student[i].usn; document.write(usn+” “+name); } } </script> <script type=“text/javascript” src=“student.json?

callback=MyJson”></script>

Raghavendra

Nayak M

Page 30: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

BUILDING A SIMPLE YOUTUBE APP

YouTube is a video hosting service from Google.

YouTube Provides various API’s to access its data

It offers data in XML, JSONC format

Raghavendra

Nayak M

Page 31: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

YOUTUBE JSON EXAMPLE

Constructing URL Base URL is

http://gdata.youtube.com/feeds/api/videos?v=2

Parameters q=<search query> callback=<function name> http://gdata.youtube.com/feeds/api/videos?v

=2&alt=jsonc&q=eminem&callback=myvideo

The first parameter to url should begin with ‘?’ and all other parameter should begin with ‘&’

Raghavendra

Nayak M

Page 32: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

MYVIDEO FUNCTION

<script type="text/javascript">function myvideo(json)  {    for(i=0;i<6;i++)    {      var title=json.data.items[i].title;      document.write(title+'<br/>');    }  }</script>

<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=eminem&callback=myvideo"></script>

Raghavendra

Nayak M

Page 33: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

EMBEDDING YOUTUBE VIDEO

YouTube Videos are Abobe flash content we can embed them using

<embed src=“FLASH URL” width=“300px” height=“200px”></embed>

Raghavendra

Nayak M

Page 34: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

FULL SOURCE CODE <script type="text/javascript">

function myvideo(json)  {    for(i=0;i<6;i++)    {      var title=json.data.items[i].title;      var video='<embed src="'+json.data.items[i].content[5]+'"/>';      document.write(title+'<br/>'+video+'</br>');    }  }</script><script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=eminem&callback=myvideo"></script>

Raghavendra

Nayak M

Page 35: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

WIKIPEDIA API

Wikipedia is a free encyclopedia which anyone can edit.

Wikipedia uses a CMS(Content Management System) know as Media Wiki.

We can perform use wikipedia suggest api, pages api and many more things

Raghavendra

Nayak M

Page 36: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

URL CONSTRUCTION

Base URL is http://en.wikipedia.org/w/api.php Parameters action=parse page=<Page Name> prop=text format=json callback=mywiki(callback function)

Raghavendra

Nayak M

Page 37: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

MYWIKI FUNCTION

<script type="text/javascript">function mywiki(json)            {        var html=[ ];         var ind = json.parse.text.*;        html.push(ind);        document.write(html);            }

</script> Above function helps us to retrieve a

wikipedia page.

Raghavendra

Nayak M

Page 38: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

COMPLETE SOURCE CODE

<script type="text/javascript">      function mywiki(json)            {        var html=[ ];        var ind = json.parse.text.*;        html.push(ind);       document.write(html);            }  </script>  <script type="text/javascript" src="http://en.wikipedia.org/w/api.php?action=parse&page=Bangalore&prop=text&format=json&callback=mywiki"></script>

Raghavendra

Nayak M

Page 39: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

TWITTER PUBLIC TIMELINE

Twitter is a micro blogging site. A list of Public Tweets is called as Public

Timeline. Public Timeline doesn’t require

Authentication to retrieve tweets.

Raghavendra

Nayak M

Page 40: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

URL CONSTRUCTION

Base URL:-http://api.twitter.com/1/statuses/public_timeline.json

Callback=<Callback Function>

Raghavendra

Nayak M

Page 41: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

PUB TWEET FUNCTION

<script type="text/javascript">      function pubtweet(json)       {        for(i=0;i<6;i++)        {          var uname=json[i].user.name       var img='<img src="'+json[i].user.profile_image_url+'"/>';       var twt=json[i].text;          document.write(img+uname+'</br>'+twt+'</br>');        }        }  </script>

Raghavendra

Nayak M

Page 42: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

COMPLETE SOURCE CODE <script type="text/javascript">

      function pubtweet(json)       {        for(i=0;i<6;i++)        {          var uname=json[i].user.name       var img='<img src="'+json[i].user.profile_image_url+'"/>';       var twt=json[i].text;          document.write(img+uname+'</br>'+twt+'</br>');        }        }  </script>  <script type="text/javascript" src="http://api.twitter.com/1/statuses/public_timeline.json?count=3&include_entities=true&callback=pubtweet"></script>

Raghavendra

Nayak M

Page 43: Introduction to JSON and Building Youtube, Twitter and Wikipedia Web Apps

QUESTIONS????

Mail me : [email protected]

Thank You

Raghavendra

Nayak M