Monday 29 March 2021

Javax.json: JsonArrayBuilder: build arrays

JsonArrayBuilder is used to construct an array of items.

 

For example, let’s see how to create an array of strings using JsonArrayBuilder.

 

Step 1: Get an instance of JsonArrayBuilder.

JsonArrayBuilder empNamesArrayBuilder = Json.createArrayBuilder();

 

Step 2: Add strings to ‘empNamesArrayBuilder’ using add method.

empNamesArrayBuilder.add("Ram");

empNamesArrayBuilder.add("Rahim");

empNamesArrayBuilder.add("Joel");

empNamesArrayBuilder.add("PTR");

                 

Step 3: Construct JsonArray.

JsonArray empNamesArr = empNamesArrayBuilder.build();

 

Step 4: Convert JsonArray to json.

String json = empNamesArr.toString();

 

Find the below working application.

 

JsonArrayBuilderDemo.java

package com.sample.app;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;

public class JsonArrayBuilderDemo {
	public static void main(String[] args) {
		JsonArrayBuilder empNamesArrayBuilder = Json.createArrayBuilder();
		
		empNamesArrayBuilder.add("Ram");
		empNamesArrayBuilder.add("Rahim");
		empNamesArrayBuilder.add("Joel");
		empNamesArrayBuilder.add("PTR");
		
		JsonArray empNamesArr = empNamesArrayBuilder.build();
		
		String json = empNamesArr.toString();
		
		System.out.println(json);
		
	}

}

Output

["Ram","Rahim","Joel","PTR"]


Let’s construct following json using JsonObjectBuilder, JsonArrayBuilder.

{
	"emps": [{
		"id": 1,
		"name": "Ram"
	}, {
		"id": 2,
		"name": "Krishna"
	}],
	"totalCount": 2
}


Step 1: Define employee JsonObjectBuilder instances.

JsonObjectBuilder emp1JsonBuilder = Json.createObjectBuilder();

emp1JsonBuilder.add("id",1).add("name", "Ram");

 

JsonObjectBuilder emp2JsonBuilder = Json.createObjectBuilder();

emp2JsonBuilder.add("id",2).add("name", "Krishna");

 

Step 2: Add employee json objects to json array.

JsonArray emps = Json.createArrayBuilder().add(emp1JsonBuilder).add(emp2JsonBuilder).build();

 

Step 3: Define emps object and totalCount property to a new JsonObjectBuilder instance.

 

JsonObjectBuilder finalObjBuilder = Json.createObjectBuilder();

finalObjBuilder.add("emps", emps);

finalObjBuilder.add("totalCount", 2);

 

Step 4: Get json object from the builder.

JsonObject finalObj = finalObjBuilder.build();

 

Step 5: Get json string from the json object.

String resultJson = finalObj.toString();

 

Find the below working application.

 

JsonArrayBuilderDemo2.java

package com.sample.app;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

/**
 * {
	"emps": [{
		"id": 1,
		"name": "Ram"
	}, {
		"id": 2,
		"name": "Krishna"
	}],
	"totalCount": 2
}
 *
 */
public class JsonArrayBuilderDemo2 {
	public static void main(String[] args) {
		JsonObjectBuilder emp1JsonBuilder = Json.createObjectBuilder();
		emp1JsonBuilder.add("id",1).add("name", "Ram");
		
		JsonObjectBuilder emp2JsonBuilder = Json.createObjectBuilder();
		emp2JsonBuilder.add("id",2).add("name", "Krishna");
		
		JsonArray emps = Json.createArrayBuilder().add(emp1JsonBuilder).add(emp2JsonBuilder).build();
		
		JsonObjectBuilder finalObjBuilder = Json.createObjectBuilder();
		finalObjBuilder.add("emps", emps);
		finalObjBuilder.add("totalCount", 2);
		
		JsonObject finalObj = finalObjBuilder.build();
		String resultJson = finalObj.toString();
		
		System.out.println(resultJson);
		
	}

}


Output

{"emps":[{"id":1,"name":"Ram"},{"id":2,"name":"Krishna"}],"totalCount":2}





 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment