JsonObject中的循环值

时间:2018-02-01 09:57:46

标签: java json javax.json

我给自己一个任务来循环JsonObject内的值;例如,我希望我的json看起来像这样:

{
        "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
            }
        ]
}

我写了这个:

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

当然jsonObject将返回循环的最后一个对象,然后单独添加

如何循环并将所有对象添加到我的arrayBuilder中?请帮助编码器!

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

首先需要创建数组构建器,添加所有json对象,然后构建数组并将其添加到根对象中:

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

答案 2 :(得分:1)

查看docs中的示例,您可以使用JsonArrayBuilder添加JsonObjects,例如:

JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
arrayBuilder.add(jsonObject);

然后,将数组添加到最终的JsonObject中,如:

JsonObject jsonParams = Json.createObjectBuilder()
                    .add("cusEmail", email)
                    ...
                    .add("orderedItems", arrayBuilder.build());