+ All Categories

Json

Date post: 08-May-2015
Category:
Upload: soumya
View: 1,509 times
Download: 1 times
Share this document with a friend
32
1 JSON (JavaScript Object Notation)
Transcript
Page 1: Json

1

JSON (JavaScript Object Notation)

Page 2: Json

2

Light weight data-interchange format.

Subset of JavaScript.

Language independent.

Text based.

Easy to parse .

What is JSON?

Page 3: Json

3

Why JSON?

JSON is recognized natively by JavaScript. No need for parsing anXML document to extract the data.

Page 4: Json

4

JSON is Not…

JSON is not a document format.

JSON is not a markup language.

Page 5: Json

5

Properties of JSON

Human and machine readable format.

No support for Unicode.

Self-documenting format that describes structure and field names.

Strict syntax and parsing requirements.

Represents the most general computer science data structures like records, lists and trees.

Page 6: Json

6

Where can JSON be used?

Can be used for transferring medium amounts of data.

Used in Java Script and then rendered on HTML pages.

AJAX has many applications for JSON.

Use JSON for applications that are browser based.

Page 7: Json

7

JSON Data Types

Strings -Control Characters

Number-Integer, Real and Floating

Boolean-True and False

Null

Page 8: Json

8

JSON Data Types (Cont…)

String

Strings must be delimited by the double quote characters.

Example: ”Address”, "444 Colombia"

Boolean

In JSON “True" and “False" are pre-defined keywords.

Example: “Active":True

Page 9: Json

9

Data types (Cont…)

Number

JSON includes positive integers and Negative integers.

Example: “Total points” -123

Null

Null string, also known as an empty string, is a string of zero length.

Example: “Email":null

Page 10: Json

10

Simple Example of a JSON File

{ "menu": "File", "commands": [ { "title": "New“, "action":"CreateDoc” }, { "title": "Open", "action": "OpenDoc” }, { "title": "Close“, "action": "CloseDoc” } ] }

Page 11: Json

11

JSON Object Notation

JSON object is an unordered set of name/value pairs and orderedlist of values.

Object begins with { (left brace) and ends with } (right brace).

Each name is followed by (colon).

Name/value pairs are separated by , (comma).

JSON Object

Page 12: Json

12

JSON Representation of an Object

Var HomeAddress ={ "Address" : “Srini", "City" : “Bangalore", "PostalCode" : 580006};

Page 13: Json

13

JSON Functions

JSON_decode — Decodes a JSON string.

JSON_encode — Returns the JSON representation of a value.

JSON_last_error — Returns the last error occurred.

Page 14: Json

14

JSON Decode

Decode parses the strings as a JSON-syntax string and returns thegeneric JSON object representation.

json_decode()

mixed json_decode ( string $json , bool $assoc)

Takes a JSON encoded string and converts it into a PHP value.

$jsonThe JSON string being decoded

$assocFalse ----Return the value as an objectTrue ----Return the value as an associative array

Page 15: Json

15

JSON Decode Example

<?php$json = '{"foo-bar": 12345}';$obj = json_decode($json);print $obj->{'foo-bar'}; // 12345?>

Page 16: Json

16

JSON Encode

JSON encode returns the JSON representation of a value.

string json_encode ( mixed $value )

Returns a string containing the JSON representation of $value.

$value

Value being encoded, Can be any type except a resource.

Function only works with UTF-8 encoded data.

Page 17: Json

17

JSON Encode Example

<?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 54321?>

Page 18: Json

18

JSON in JavaScript

JavaScript Object Notation is a lightweight data-interchangeformat that is completely language independent text formatwhich is fast and easy to understand.

Page 19: Json

19

Example - JSON in JavaScript

<html><head><title>Object creation in JSON in JavaScript</title><script language="javascript" >var JSONObject = {"name" : "Amit“, “address" : "Bangalore“,"age" : 23, "phone" : "4565763",“MobileNo" : 981100092};

Page 20: Json

20

document.write("<h2><font color='skyblue'>Name</font>::“+JSONObject.name+"</h2>");document.write("<h2><font color='skyblue'>Address</font>::“+JSONObject.address+"</h2>");document.write("<h2><font color='skyblue'>Age</font>::“+JSONObject.age+"</h2>");document.write("<h2><font color='skyblue'>Phone No.</font>::“+JSONObject.phone+"</h2>");document.write("<h2><font color='skyblue'>Mobile No.</font>::“+JSONObject.MobileNo+"</h2>");</script></head><body bgcolor="#cc4488"><h3>Example of object creation in JSON in JavaScript</h3></body></html>

Example - JSON in JavaScript (Cont…)

Page 21: Json

21

Both XML and JSON use structured approaches to mark up data.

More and more web services are supporting JSON.

Example

Yahoo's various search services, travel planners, and highwaytraffic services.

AJAX with JSON

Page 22: Json

22

JSON is widely used in AJAX. The X in AJAX stands for XML.

Example

{"fullname": "Swati Kumar“, "org": "Columbia",}<?xml version='1.0‘ encoding='UTF-8'?><element><fullname>Swati Kumar</fullname><org>Columbia</org></element>

Why JSON is Better than Ajax?

Page 23: Json

23

Benefits of JSON

The easiness of reading

The easiness of using

Benefits of XML

XML is extensible.

Widely used and recognized by almost all programming languages.

JSON and XML

Unfortunally, both XML and JSON are enable to integrate a largeamount of data in binary form.

Page 24: Json

24

{"firstName": “Sahana","lastName": "Saniya","age": 25,"address": {"streetAddress": “2nd Street","city": “Bangalore","state": “India",},"phoneNumber": [{ "type": "home", "number": “234243" },{ "type": "fax", "number": “645654" }],"newSubscription": false,"companyName": null}

Example

Page 25: Json

25

<Person><firstName>John</firstName><lastName>Smith</lastName><age>25</age><address><streetAddress>21 2nd Street</streetAddress><city>New York</city><state>NY</state><postalCode>10021</postalCode></address><phoneNumber type="home">212 555-1234</phoneNumber><phoneNumber type="fax">646 555-4567</phoneNumber><newSubscription>false</newSubscription><companyName /></Person>

Equivalent for the above in XML could be

Page 26: Json

26

Both are self-describing and thus human readable.

Both are hierarchical (i.e. we can have values within values.)

Both can be parsed and used by lots of programming languages.

Both can be passed around using AJAX (i.e. httpWebRequest).

JSON is Like XML because…

Page 27: Json

27

JSON stands for JavaScript Object Notation, Considered as thesubset of JavaScript.

XML on the other hand is a mark-up language that could be used indifferent languages.

Parsing JSON encoded data is much faster than parsing XMLencoded data.

JSON Vs XML

Page 28: Json

28

XML<result><people><person firstName=“Aman" lastName=“Valya"/><person firstName=“Suman" lastName=“Valya"/>...</people></result>

JSON{people:[{ 'firstName': ‘Aman', 'lastName', ‘Valya' },{ 'firstName': ‘Suman', 'lastName', ‘Valya' },...

JSON Vs XML (Cont…)

Page 29: Json

29

Lighter and faster than XML as on-the-wire data.

JSON objects are typed while XML data is type less.

JSON types are string, number, array, Boolean.

XML data are all string.

Data is readily accessible as JSON objects in your JavaScript Code.

XML data needed to be parsed and assigned to variables throughtedious DOM APIs.

Why Use JSON Over XML?

Page 30: Json

30

Provides multiple functionalities such as encode, decode/parse.

Flexible, simple and easy to use.

Supports streaming output of JSON text.

Heap based parser.

High performance.

No dependency on external libraries.

Features of JSON

Page 31: Json

31

JSON is a simple, common representation of data.

Communication between servers and browser clients.

Communication between peers.

Language independent data interchange.

JSON Data Interchange

Page 32: Json

32

Thank you


Recommended