Reputation: 540
I have given myself a task to loop values inside an JsonObject; for instance, I want my json to look like this:
{
"cusEmail": "...",
"totalQuantity": 11,
"totalCost": 296.89,
"phoneNumber": "844 463 9211",
"address": "...",
"closeShop": "...",
"contact": "By email: and phone call::",
"status": "...",
"orderDate": "2017-15-12",
"deliveryDate": "2017-12-18",
"orderedItems": [
{
"name": "Blue Ribbon",
"manufacture": "Blue Ribbon Brown Plus Low Gi Bread 700g",
"price": 13.99,
"image": "data:image/jpeg;base64,..."
"count": 1
},
{
"name": "Cheese",
"manufacture": "Galbani Mozzarella Cheese 300g",
"price": 49.99,
"image": "data:image/octet-stream;base64,..."
"count": 1
}
]
}
And I have written this:
int cartSize = ShoppingCartController.getInstance().myProducts.size();
String[] nameString = new String[cartSize];
String[] imageString = new String[cartSize];
Integer[] qauntityInteger = new Integer[cartSize];
String[] manufactureString = new String[cartSize];
Double[] priceDouble = new Double[cartSize];
JsonObjectBuilder jsonObject = null;
for ( int i = 0; i < cartSize; i++ ){
Item myProducts = ShoppingCartController.getInstance().myProducts.get(i);
nameString[i] = myProducts.getName();
manufactureString[i] = myProducts.getManufacture();
priceDouble[i] = myProducts.getPrice();
imageString[i] = myProducts.getImage();
qauntityInteger[i] = myProducts.getQuantity();
jsonObject = Json.createObjectBuilder()
.add("name", nameString[i])
.add("manufacture", manufactureString[i])
.add("price", priceDouble[i])
.add("image", imageString[i])
.add("count", qauntityInteger[i]);
}
JsonObject jsonParams = Json.createObjectBuilder()
.add("cusEmail", email)
.add("totalQuantity", quantity)
.add("totalCost", totalCost)
.add("phoneNumber", phoneNumber)
.add("address", address)
.add("closeShop", closeShop)
.add("contact", contact)
.add("status", "ordered")
.add("orderDate", dateFormat.format(orderDate))
.add("deliveryDate", delDate)
.add("orderedItems", Json.createArrayBuilder()
.add(jsonObject))
.build();
Of course jsonObject will return the last object of the loop, which will then be added alone.
How will I loop and add all objects into my arrayBuilder?
Upvotes: 0
Views: 1200
Reputation: 480
Looking at the example in the docs, you can have a JsonArrayBuilder, to which you can add your JsonObjects, like:
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
arrayBuilder.add(jsonObject);
Then, add the array to the final JsonObject, like:
JsonObject jsonParams = Json.createObjectBuilder()
.add("cusEmail", email)
...
.add("orderedItems", arrayBuilder.build());
Upvotes: 1
Reputation: 422
JSONObject cart = new JSONObject();
cart.put("cusEmail", email);
cart.put("totalQuantity", quantity);
cart.put("totalCost", totalCost);
cart.put("phoneNumber", phoneNumber);
cart.put("address", address);
cart.put("closeShop", closeShop);
cart.put("contact", contact);
cart.put("status", "ordered");
cart.put("orderDate", dateFormat.format(orderDate));
cart.put("deliveryDate", delDate);
JSONArray objects = new JSONArray();
JSONObject obj;
for (int i = 0; i < 5; i++) {
obj = new JSONObject();
obj.put("name", nameString[i]);
obj.put("manufacture", manufactureString[i]);
obj.put("price", priceDouble[i]);
obj.put("image", imageString[i]);
obj.put("count", qauntityInteger[i]);
objects.add(obj);
}
cart.put("orderedItems", objects);
Upvotes: 2
Reputation: 14328
You need to create the array builder first, add all json objects, and then build the array and add it to the root object:
JsonArrayBuilder array = Json.createArrayBuilder();
for ( int i = 0; i < cartSize; i++ ){
...
array.add(Json.createObjectBuilder()
.add("name", nameString[i])
.add("manufacture", manufactureString[i])
.add("price", priceDouble[i])
.add("image", imageString[i])
.add("count", qauntityInteger[i]));
}
JsonObject jsonParams = Json.createObjectBuilder()
.add("cusEmail", email)
.add("totalQuantity", quantity)
.add("totalCost", totalCost)
.add("phoneNumber", phoneNumber)
.add("address", address)
.add("closeShop", closeShop)
.add("contact", contact)
.add("status", "ordered")
.add("orderDate", dateFormat.format(orderDate))
.add("deliveryDate", delDate)
.add("orderedItems", array.build())
.build();
Upvotes: 1