Thursday, 20 November 2014

HOW TO BUILD JSON STRUCTURE IN JAVA EE 7 USING JSON-PROCESSING (JSON-P)


JSON is just like XML. 

JSON-P provides two different programming models to process JSON documents:

* Object Model API            * Streaming API

Object Model API is similar to DOM API for XML. It provides classes to model
JSON objects and arrays in a treelike structure that represent JSON data in memory.

To create this tree structure : we use the Builder classes

* Class JsonObjectBuilder -> used for creating a JSON object & returns  JsonObject object instance.

* JsonArrayBuilder - used for creating a JSON Array (might be inclusive to a JsonObject or a standalone JsonArray)  and returns a JsonArray object instance.

* All the classes :

Json
JsonObject
JsonArray
JsonObjectBuilder
JsonArrayBuilder

are available under the package: javax.json

Lets take an example:

This example is about a student and his skills.
{
  "student" : {
            "id" : "999",
            "name" : "dinesh",
            "address" : {
                "city" : "hyderabad"
                "country" : "india"
              },
             "skills": {
                 "languages": [
                     {
                        "language" :  "english"
                        "reading" : "good"
                        "writing": "average"
                        "speaking" : "excellent"
                    },
                    {

                        "language" :  "telugu"
                        "reading" : "good"
                        "writing": "average"
                        "speaking" : "good"
                    }
                  ]
           }
      }
  }

Note:

JSON structures:

 *Any thing included in { } is considered as a JsonObject


 *Any thing included  in [ ]  is considered as a JsonArray


 * Also, note that a JsonObject may internally can have any number of other 

Json objects and Arrays.

*Even, Json arrays that are included in [ ] brackets are also internally included in
json objects, means array elements must be included in json objects i.e, { }



Now, let us get some hands-on...

//this class has many factory methods useful for object , array creations
import javax.json.Json;

 import javax.json.JsonObject;

 import javax.json,JsonArray;


public class StudentInfoBuilder { public JsonObject buildStudentInfo() {


return Json.createObjectBuilder().add("student", Json.createObjectBuilder()
                 .add("id", "999")
                 .add("name", "dinesh")
                 .add("address", Json.createObjectBuilder()
                                                   .add("city", "hyderabad")
                                                   .add("country", "india"))
                 .add("skills", Json.createObjectBuilder()
                                             .add("languages", Json.createArrayBuilder()
                                                                  .add(Json.createObjectBuilder()
                                                                                    .add("language", "english")
                                                                                    .add("reading", "good")
                                                                                    .add("writing", "average")
                                                                                    .add("speaking", "excellent"))
                                                                  .add(Json.createObjectBuilder()
                                                                                    .add("language", "telugu")
                                                                                    .add("reading", "good")
                                                                                    .add("writing", "average")

                                                                                    .add("speaking", "good"))
                                          )
                 )).build();
          }

 }
 

Json.createObjectBuilder() method is used to build a JsonObject

Json.createArrayBuilder() method is used to build a JsonArray

.add() method for adding  the respective elements for either an object  / array.


Hope this is helpful....