I have a method that should return a Response
with a JsonObject
(with arraylist) exactly like the code at the bottem of this article. This is a java @GET
method. I know how to build a jsonobject
with json.createObjectBuilder
like:
JsonObject jo = Json.createObjectBuilder().add("name", "item").add("user", user.getUser()).build();
But I dont how to build like the code below. So I have to do an add with the name "items", this is an arraylist. Every item has four attributes: id, name, bool and another arraylist reserveItems (reserveItems can be null). After this I have to do an add with name length and value 687.
{
"items" :[
{
"id" : 1,
"name" : "Item1",
"bool" : true,
"reserveItems": []
},
{
"id" : 2,
"name" : "Item2",
"bool" : false,
"reserveItem": []
}
],
"length" :687
}
From the JsonObjectBuilder interface, you can obtain your JsonObject in this way:
JsonObject value = Json.createObjectBuilder()
.add("items", Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("id", 1)
.add("name", "Item1")
.add("bool", true)
.add("reserveItems", Json.createArrayBuilder())
)
.add(Json.createObjectBuilder()
.add("id", 2)
.add("name", "Item2")
.add("bool", false)
.add("reserveItems", Json.createArrayBuilder())
)
)
.add("length", 687)
.build();
System.out.println(value);