Creating & Parsing JSON data with Java Servlet/Struts/JSP

      

java logo
JSON (JavaScript Object Notation) is a lightweight computer data interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (called objects). The JSON format is specified in RFC 4627 by Douglas Crockford. The official Internet media type for JSON is application/json.

The JSON format is often used for transmitting structured data over a network connection in a process called serialization. Its main application is in AJAX web application programming, where it serves as an alternative to the traditional use of the XML format.

Supported data types

  1. Number (integer, real, or floating point)
  2. String (double-quoted Unicode with backslash escapement)
  3. Boolean (true and false)
  4. Array (an ordered sequence of values, comma-separated and enclosed in square brackets)
  5. Object (collection of key/value pairs, comma-separated and enclosed in curly brackets)
  6. null

Syntax

The following example shows the JSON representation of an object that describes a person. The object has string fields for first name and last name, contains an object representing the person’s address, and contains a list of phone numbers (an array).

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

Creating JSON data in Java

JSON.org has provided libraries to create/parse JSON data through Java code. These libraries can be used in any Java/J2EE project including Servlet, Struts, JSF, JSP etc and JSON data can be created.

Download JAR file json-rpc-1.0.jar (75 kb)

Use JSONObject class to create JSON data in Java. A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get() and opt() methods for accessing the values by name, and put() methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.

import org.json.JSONObject;

...
...

JSONObject json = new JSONObject();
json.put("city", "Mumbai");
json.put("country", "India");

...

String output = json.toString();

...

Thus by using toString() method you can get the output in JSON format.

JSON Array in Java

A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values. The internal form is an object having get and opt methods for accessing the values by index, and put methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object.

The constructor can convert a JSON text into a Java object. The toString method converts to JSON text.

JSONArray class can also be used to convert a collection of Java beans into JSON data. Similar to JSONObject, JSONArray has a put() method that can be used to put a collection into JSON object.

Thus by using JSONArray you can handle any type of data and convert corresponding JSON output.


Facebook  Twitter      Stumbleupon  Delicious
  

7 Comments on “Creating & Parsing JSON data with Java Servlet/Struts/JSP”

  • Navin wrote on 3 June, 2009, 17:33

    Hi Team,
    I want to know how JSON helps in file upload in Java/Servlet/JSP.
    Sample code will be appriciated.

    regards,
    K. Navin.

  • npk wrote on 15 June, 2009, 7:48

    Hi Team,

    I need to have a simple example of the below
    1) Simple JSON example with Servlet
    2) Fileupload using JSON & Servlet

    Thanks for your help.

  • Dharmesh wrote on 30 July, 2009, 17:19

    Lot of questions left unanswered in this post.. was looking for something more elaborate .. but its ok .. will google it elsewhere

  • Ratna Dinakar wrote on 28 October, 2009, 9:08

    Instead of JSON RPC Jar.. try using this JSON-Lib. It has many advance features compared to library of json.org.

  • Imran wrote on 13 November, 2009, 17:00

    Hi ,

    Im trying to write a servlet that returns geoJSON, do I need to use the same library or does anybody have an idea how to convert JTS Geometry directly into JSON?

    thanks
    Imran
    Lahore, Pakistan

  • chirag Patil wrote on 6 March, 2010, 17:50

    can anyone have JSON java tutorial with output?
    If anyone has then plz contact me on patil.boss@gmail.com

  • Hitesh jain wrote on 26 May, 2010, 13:36

    Hi,
    I need to use json object in java and in javascript and in jsp….can any one help me how to populate it on the jsp. ASAP

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 ViralPatel.net. All rights reserved.