You are on page 1of 30

Introduction to

Introduction to
JSON (JavaScript
JSON (JavaScript
Object Notation)
Object Notation)
Sang Shin Sang Shin
Java Technology Architect Java Technology Architect
Sun Microsystems, Inc. Sun Microsystems, Inc.
sang.shin@sun.com sang.shin@sun.com
www.javapassion.com www.javapassion.com
2
Disclaimer & Acknowledgments

Even though Sang Shin is a full-time employee of


Sun Microsystems, the contents here are created as
his own personal endeavor and thus does not reflect
any official stance of Sun Microsystems
3
opics
What is JSON?
JSON ata Structure
>
JSON O!"ect
>
JSON te#t
JSON and Java $echnology
%ow to send and receive JSON data at !oth client and
server sides
JSON-&'(
&esources

!"at is & !"# JSON$
!"at is & !"# JSON$
5
!"at is JSON$
)ightweight data-interchange format
>
(ompared to *M)
Simple format
>
Easy for humans to read and write
>
Easy for machines to parse and generate
JSON is a te#t format
>
'rogramming language independent
>
+ses conventions that are familiar to programmers of the (-
family of languages, including (, (,,, (-, Java, JavaScript,
'erl, 'ython
6
!"# %se JSON over &'(
)ighter and faster than *M) as on-the-wire data
format
JSON o!"ects are typed while *M) data is typeless
>
JSON types. string, num!er, array, !oolean,
>
*M) data are all string
Native data form for JavaScript code
> ata is readily accessi!le as JSON o!"ects in your JavaScript
code vs/ *M) data needed to !e parsed and assigned to
varia!les through tedious OM 0'1s
>
&etrieving values is as easy as reading from an o!"ect
property in your JavaScript code
7
!"ere is JSON %sed$
&epresent configuration information
1mplement communication protocols

JSON Object
JSON Object
9
JSON Structures
0 collection of name2value pairs
>
1n various languages, this is reali3ed as an o!"ect, record,
struct, dictionary, hash ta!le, 4eyed list, or associative array
0n ordered list of values
>
1n most languages, this is reali3ed as an array, vector, list, or
se5uence
$hese are universal data structures supported !y
most modern programming languages
10
JSON Object Notation
0 JSON o!"ect is an unordered set of name2value
pairs
0 JSON o!"ect !egins with 6 7left !race8 and ends
with 9 7right !race8
Each name is followed !y . 7colon8 and the
name2value pairs are separated !y , 7comma8
11
JSON and JavaScript
JSON is a su!set of the o!"ect literal notation of
JavaScript
>
JSON can !e used in the JavaScript language with no muss
or fuss
12
)*ample+ JSON Object
var myJSONO!"ect : 6;!indings;. <
6;ircEvent;. ;'&1=MS>;, ;method;. ;new+&1;, ;rege#;. ;?http.22/@;9,
6;ircEvent;. ;'&1=MS>;, ;method;. ;delete+&1;, ;rege#;. ;?delete/@;9,
6;ircEvent;. ;'&1=MS>;, ;method;. ;random+&1;, ;rege#;. ;?random/@;9
A
9B
1n this e#ample, a JSON JavaScript o!"ect is created
containing a single mem!er ;!indings;, which contains
an array containing three o!"ects, each containing
;ircEvent;, ;method;, and ;rege#; mem!ers
Mem!ers can !e retrieved using dot or su!script
operators
myJSONO!"ect/!indings<CA/method 22 ;new+&1;
13
e*t to Object ,onversion in
JavaScript code
var myO!"ect : eval7D7D , myJSONte#t , D8D8B
$o convert a JSON te#t into an JSON o!"ect, use
the eval78 function
>
eval78 invo4es the JavaScript compiler
>
Since JSON is a proper su!set of JavaScript, the compiler will
correctly parse the te#t and produce an o!"ect structure
14
Securit# & JSON -arser
22 1nclude http.22www/"son/org2"son/"s
var myO!"ect : myJSONte#t/parseJSON78B
eval78 can compile and e#ecute any JavaScript
program, so there can !e security issues 7cross-site
scripting8
>
+se eval78 when the source can !e trusted
When security is a concern - the source cannot !e
trusted -, it is !etter to use a JSON parser
>
0 JSON parser will only recogni3e JSON te#t and so is much
safer
15
Object to e*t ,onversion
var myJSON$e#t : myO!"ect/toJSONString78B
Eou can convert JSON o!"ect into JSON te#t
JSON does not support cyclic data structure
> o not give cyclical structures to the JSON stringifier

JSON in Java
JSON in Java
17
JSON ools .or Java Developer
'arser
>
'arse JSON te#t files and convert these to a Java model
&enderer
>
&ender a Java representation into te#t
Seriali3er
>
Seriali3e plain 'OJO clusters to a JSON representation
=alidator
>
=alidate the contents of a JSON file using a JSON schema
18
JSONObject Java ,lass
0 JSONO!"ect is an unordered collection of
name2value pairs
$he put methods adds a name2value pair to an
o!"ect
$he te#ts produced !y the toString methods strictly
conform to the JSON synta# rules
myString : new JSONO!"ect78/put7;JSON;, ;%ello,
WorldF;8/toString78B
22 myString is 6;JSON;. ;%ello, World;9

/ow to Send & 0eceive
/ow to Send & 0eceive
JSON Data at 1ot"
JSON Data at 1ot"
,lient and Server Side
,lient and Server Side
20
/ow to 2enerate3Send JSON Data
at t"e Server Side
(reate JSONO!"ect Java o!"ect
0dd name2value pairs using put method
(onvert it to String type using toString method and
send it to the client with content-type as Gte#t2#mlH or
Gte#t2plainH
myString : new JSONO!"ect78/put7;JSON;, ;%ello,
WorldF;8/toString78B
22 myString is 6;JSON;. ;%ello, World;9
21
/ow to 0eceive JSON Data at t"e
,lient Side
JSON data is received as a string
(alling eval78 will generate JSON o!"ect in
JavaScript code
>
var JSONdata : eval7re5/response$e#t8B
Once you have JSON o!"ect, you can use / notation
to access its properties
>
var name : JSONdata/nameB
>
var address : JSONdata/addresses<IAB
>
var streetname : JSONdata/addresses<IA/streetB
22
/ow to 2enerate3Send JSON Data
at t"e ,lient Side
(reate JSON JavaScript o!"ect
+se G'OS$H %$$' method in the open method of the
*M)%ttp&e5uest o!"ect
'ass JSON JavaScript o!"ect in the send method of
*M)%ttp&e5uest o!"ect

var car0sJSON : JSON/stringify7car8B
var url : ;JSONE#ample?timeStamp:; , new ate78/get$ime78B
create*M)%ttp&e5uest78B
#ml%ttp/open7;'OS$;, url, true8B
#ml%ttp/onreadystatechange : handleState(hangeB
#ml%ttp/set&e5uest%eader7;(ontent-$ype;,
;application2#-www-form-urlencoded;8B
#ml%ttp/send7car0sJSON8B
23
/ow to 0eceive JSON Data at t"e
Server Side
&ead the JSON data as a String type
(reate JSONO!"ect Java o!"ect from the string
String "son : readJSONStringJrom&e5uestKody7re5uest8B
22+se the JSON-Java !inding li!rary to create a JSON o!"ect in Java
JSONO!"ect "sonO!"ect : nullB
try 6
"sonO!"ect : new JSONO!"ect7"son8B
9
catch7'arseE#ception pe8 6
9

JSON40-, &
JSON40-, &
JSON40-,4Java
JSON40-,4Java
25
!"at is JSON40-,$ !"at is
JSON40-,4Java$
JSON-&'( is a simple remote procedure call
protocol similar to *M)-&'( although it uses the
lightweight JSON format instead of *M)
JSON-&'(-Java is a Java implementation of the
JSON-&'( protocol
26
!"# JSON40-,4Java$
1t allows you to transparently call server-side Java
code from JavaScript with an included lightweight
JSON-&'( JavaScript client
1t is designed to run in a Servlet container such as
$omcat and can !e used with JLEE 0pplication
servers to allow calling of plain Java or EJK methods
from within a JavaScript %$M) we! application
27
5eatures o. JSON40-,4Java
ynamically call server-side Java methods from JavaScript
%$M) we! applications/ No 'age reloading/
0synchronous communications/
$ransparently maps Java o!"ects to JavaScript o!"ects/
)ightweight protocol similar to *M)-&'( although much
faster/
)everages JLEE security model with session specific
e#porting of o!"ects/
Supports 1nternet E#plorer, Mo3illa, Jirefo#, Safari, Opera and
Mon5ueror

0esources
0esources
29
JSON 0esources
1ntroducing JSON
>
http.22www/"son/org2
JSON in JavaScript
>
http.22www/"son/org2"s/html
JSON in Java
>
http.22www/"son/org2"ava2inde#/html

Introduction to
Introduction to
JSON (JavaScript
JSON (JavaScript
Object Notation)
Object Notation)
Sang Shin Sang Shin
Java Technology Architect Java Technology Architect
Sun Microsystems, Inc. Sun Microsystems, Inc.
sang.shin@sun.com sang.shin@sun.com
www.javapassion.com www.javapassion.com

You might also like