JEE7 JSON: How to use JEE 7 JSON API?
In this tutorial I will explain you how you can use JEE 7 JSON API in your program for consuming and generating the JSON data on fly. The JSON stands for JavaScript Object Notation. The JSON is JavaScript standard but it can be used with almost all the major programming languages.
Here we are talking about the the Java API for JSON Processing which is based on the specification defined in the JSR-353. JSON is used to transfer the data over the Internet and it is widely used as a data exchange format. The JSR-353 specification provides APS's for parseing, transforming, and querying the JSON data using the object model. It is also supports the streaming model for processing the JSON data.
The interface JsonArrayBuilder is used to create JsonArray models from scratch.
This interface (interface JsonArrayBuilder) is used to initializes an empty JSON array model. It also provides the necessary methods to add values to the array model. Finally it returns the resulting array. You can add multiple values to the array. In this example we are adding 4 values.
Example given here will teach you how to run the example using Maven 3. We have configured the required dependencies in the pom.xml file.
Here is the Java code (JsonArrayBuilderExample.java) of the program:
package net.roseindia; // import import javax.json.*; import java.util.*; public class JsonArrayBuilderExample { public static void main(String[] args) { // Create object of JsonArrayBuilder JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); jsonArrayBuilder.add( Json.createObjectBuilder() .add("id", "I001") .add("name", "Deepak") ); jsonArrayBuilder.add( Json.createObjectBuilder() .add("id", "I002") .add("name", "John") ); jsonArrayBuilder.add( Json.createObjectBuilder() .add("id", "I003") .add("name", "Mohan") ); jsonArrayBuilder.add( Json.createObjectBuilder() .add("id", "I004") .add("name", "Sandeep") ); JsonArray usersJson = jsonArrayBuilder.build(); System.out.println(usersJson.toString()); /* //Compile mvn compile //Run: mvn exec:java -Dexec.mainClass="net.roseindia.JsonArrayBuilderExample" //Output of the program [ {"id":"I001","name":"Deepak"}, {"id":"I002","name":"John"}, {"id":"I003","name":"Mohan"}, {"id":"I004","name":"Sandeep"} ] */ } }
In the above example following code is used to build and return the current array:
JsonArray usersJson = jsonArrayBuilder.build();
We have used the Maven 3 to compile and execute the example. To compile the code use the following command:
mvn compile
To run the example you should use the following command:
mvn exec:java -Dexec.mainClass="net.roseindia.JsonArrayBuilderExample"
The following method will print the JSON data on console:
System.out.println(usersJson.toString());
Following is the output of the program:
[ {"id":"I001","name":"Deepak"}, {"id":"I002","name":"John"}, {"id":"I003","name":"Mohan"}, {"id":"I004","name":"Sandeep"} ]
Here is the code of pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.roseindia</groupId> <artifactId>javaee7-examples</artifactId> <version>7.0</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.json</groupId> <artifactId>javax.json-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.json</artifactId> <version>1.0</version> <scope>runtime</scope> </dependency> </dependencies> <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <failOnMissingWebXml>false</failOnMissingWebXml> </properties> </project>
Download the complete project along with the code described here.