Search code examples
javajsonjavax.json

Looping values in JsonObject


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?


Solution

  • 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());