JSON Structures
• A collection of name/value pairs
> In various languages, this is realized as an object, record,

struct, dictionary, hash table, keyed list, or associative array

• An ordered list of values
> In most languages, this is realized as an array, vector, list, or

sequence

• These are universal data structures supported by most modern programming languages

9

JSON Object Notation
• A JSON object is an unordered set of name/value pairs • A JSON object begins with { (left brace) and ends with } (right brace) • Each name is followed by : (colon) and the name/value pairs are separated by , (comma)

10

JSON and JavaScript
• JSON is a subset of the object literal notation of JavaScript
> JSON can be used in the JavaScript language with no muss

• 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"

• Validator
> Validate the contents of a JSON file using a JSON schema

17

JSONObject Java Class
• A JSONObject is an unordered collection of name/value pairs • The put methods adds a name/value pair to an object • The texts produced by the toString methods strictly conform to the JSON syntax rules
myString = new JSONObject().put("JSON", "Hello, World!").toString(); // myString is {"JSON": "Hello, World"}
18

How to Send & Receive JSON Data at Both Client and Server Side

How to Generate/Send JSON Data at the Server Side
• Create JSONObject Java object • Add name/value pairs using put method • Convert it to String type using toString method and send it to the client with content-type as “text/xml” or “text/plain”
myString = new JSONObject().put("JSON", "Hello, World!").toString(); // myString is {"JSON": "Hello, World"}
20

How to Receive JSON Data at the Client Side
• JSON data is received as a string • Calling eval() will generate JSON object in JavaScript code
> var JSONdata = eval(req.responseText);

• 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;

What is JSON-RPC? What is JSON-RPC-Java?
• JSON-RPC is a simple remote procedure call protocol similar to XML-RPC although it uses the lightweight JSON format instead of XML • JSON-RPC-Java is a Java implementation of the JSON-RPC protocol

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