Introduction To JSON
Introduction To JSON
Sang Shin Java Technology Architect Sun Microsystems, Inc. sang.shin@sun.com www.javapassion.com
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
Topics
What is JSON? JSON Data Structure
> JSON Object > JSON text
JSON and Java Technology How to send and receive JSON data at both client and server sides JSON-RPC Resources
3
What is JSON?
Lightweight data-interchange format
> Compared to XML
Simple format
> Easy for humans to read and write > Easy for machines to parse and generate
code vs. XML data needed to be parsed and assigned to variables through tedious DOM APIs > Retrieving values is as easy as reading from an object property in your JavaScript code
JSON Object
JSON Structures
A collection of name/value pairs
> In various languages, this is realized as an object, record,
sequence
These are universal data structures supported by most modern programming languages
10
or fuss
11
In this example, a JSON JavaScript object is created containing a single member "bindings", which contains an array containing three objects, each containing "ircEvent", "method", and "regex" members Members can be retrieved using dot or subscript operators
myJSONObject.bindings[0].method // "newURI"
12
To convert a JSON text into an JSON object, use the eval() function
> eval() invokes the JavaScript compiler > Since JSON is a proper subset of JavaScript, the compiler will
13
eval() can compile and execute any JavaScript program, so there can be security issues (cross-site scripting)
> Use eval() when the source can be trusted
When security is a concern - the source cannot be trusted -, it is better to use a JSON parser
> A JSON parser will only recognize JSON text and so is much
safer
14
You can convert JSON object into JSON text JSON does not support cyclic data structure
> Do not give cyclical structures to the JSON stringifier
15
JSON in Java
Renderer
> Render a Java representation into text
Serializer
> Serialize plain POJO clusters to a JSON representation
Validator
> Validate the contents of a JSON file using a JSON schema
17
How to Send & Receive JSON Data at Both Client and Server Side
Once you have JSON object, you can use . notation to access its properties
> var name = JSONdata.name; > var address = JSONdata.addresses[3]; > var streetname = JSONdata.addresses[3].street;
21
25
Why JSON-RPC-Java?
It allows you to transparently call server-side Java code from JavaScript with an included lightweight JSON-RPC JavaScript client It is designed to run in a Servlet container such as Tomcat and can be used with J2EE Application servers to allow calling of plain Java or EJB methods from within a JavaScript DHTML web application
26
Features of JSON-RPC-Java
Dynamically call server-side Java methods from JavaScript DHTML web applications. No Page reloading. Asynchronous communications. Transparently maps Java objects to JavaScript objects. Lightweight protocol similar to XML-RPC although much faster. Leverages J2EE security model with session specific exporting of objects. Supports Internet Explorer, Mozilla, Firefox, Safari, Opera and Konqueror
27
Resources
JSON Resources
Introducing JSON
> http://www.json.org/
JSON in JavaScript
> http://www.json.org/js.html
JSON in Java
> http://www.json.org/java/index.html
29