Top Banner
JSON JavaScript Object Notation Samuel Chen
26

JSON Syntax

Dec 17, 2014

Download

Software

Samuel Chen

An introduction for JSON syntax around 2007
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JSON Syntax

JSON

JavaScript Object Notation

Samuel Chen

Page 2: JSON Syntax

Agenda

• Introduction• Benefits• Gramma• Native JSON• Security• Limitation• References• Example• Q & A

Page 3: JSON Syntax

Introduction     What's JSON

Page 4: JSON Syntax

JSON isa lightweight text format for data-interchange

• Easy for human to read and write.• Easy for machine to parse and generate.• Completely language independent.• Easy to uses in most programming languages.• Based on a subset of the

JavaScript Programming Language, Standard ECMA-262 3rd Edition December 1999.

Page 5: JSON Syntax

JSON is a lightweight text format for data-interchange

• A collection of name/value pairs.• An ordered list of values.• Often used for serialization and transmitting

structured data over a network connection• One of the major technologies in AJAX web

application programming.• An alternative to the XML format.

Page 6: JSON Syntax

Comparison The fat-free alternative to XML

Page 7: JSON Syntax

JSON 

{     "firstName": "John",     "lastName": "Smith",     "address": {         "streetAddress": "21 2nd Street",         "city": "New York",         "state": "NY",         "postalCode": "10021"     },     "phoneNumbers": [         { "type": "home", "number": "212 555-1234" },         { "type": "fax", "number": "646 555-4567" }     ] }

Page 8: JSON Syntax

XML 

<?xml version="1.0" encoding="utf-8"?><Person firstName="John" lastName="Smith">  <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></Person>

Page 9: JSON Syntax

BothJSON & XML

JSON XML

Text based * *

Position independent * *

Simplicity ** *

Interoperability * **

Page 10: JSON Syntax

AdvantageJSON vs. XML

• Simple• Small (228)• Built into language• Data Oriented

JSON XML• Complex• Large (310)• External parser• Document Oriented

Page 11: JSON Syntax

DisadvantageJSON vs. XML

• No Binary • None Extensible• None Display

Capability

JSON XML• <[CDATA[]]>• Good Extensibility• Many views for one

data

* Actually all these disadvantages are the design purposes.* Link - "JSON: The Fat-Free Alternative to XML"

Page 12: JSON Syntax

Gramma Data types and syntax

Page 13: JSON Syntax

Data typesValue

• Number (integer, real, or floating point)• String (double-quoted Unicode with backslash escaping)• Boolean (true and false)• Array (an ordered sequence of values, comma-separated and

enclosed in square brackets)• Object (collection of key:value pairs, comma-separated and

enclosed in curly braces)• null

Page 14: JSON Syntax

String 

• Unicode• Wrapped in double quotes• Backslash escapes• Without double quotes• Char is string

Page 15: JSON Syntax

Number 

• Int: 123, -456, 0• Real: 123.456, -3.14, 0.0• Float: 1.434E+1, 0.234e-1• Without double quotes: "123" is not number• *Octal or Hex leading, zero leading are not allowed

Page 16: JSON Syntax

Literal true, false & null

• Lowercase• Only "true", "false" and "null"• *Boolean concept

o excalmatory mark "!"o true = !falseo !null

Page 17: JSON Syntax

Object 

• Wrapped in curly brackets "{" and "}"• Zero or more name/value pairs or members• Single colon after name to separate name from value.• Single comma after value to separate a following name.• The name within the object should be unique.• * Double quotes surroundding a name is not required.

{"name":"Sam"} and {name:"Sam"} both work.• * {"123":123}, {"#%$!":"correct?"}• * No operations. Compare to object in OO.

Page 18: JSON Syntax

Array 

• Surrounded with square brackets "[" and "]"• Zero or more values separated by comma• *Array items are able to be different types.

Page 19: JSON Syntax

Security Javascript Tag Injection

Page 20: JSON Syntax

Code Injection

• JSON-formated text is also syntactically legal javascript code

• Built-in eval() is easy to use• eval("javascript:alert('Hello, World');")• Validate before eval

Page 21: JSON Syntax

Native JSON IE8, Firefox3.1, WebKits-based Browsers

Page 22: JSON Syntax

Native JSON 

• Safe• Much Faster• Object to JSON string• JSON.Parse(string)• JSON.stringify(object)

Page 23: JSON Syntax

Limitation No sementics associated

Page 24: JSON Syntax

References 

• JSON Home Page• RFC 4627 (fomal JSON spec)• The limitation of JSON• Native JSON in Firefox 3.1• Native JSON in IE8• JSON on Wiki

Page 25: JSON Syntax

Example To update a Web DataWindow

Page 26: JSON Syntax

Q & A

Thanks

[email protected]