gpt4 book ai didi

java - 在 JsonObject 中循环值

转载 作者:行者123 更新时间:2023-12-01 17:52:25 25 4
gpt4 key购买 nike

我给自己分配了一个任务,在 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 中?

最佳答案

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

关于java - 在 JsonObject 中循环值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48559882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com