【发布时间】:2017-06-24 12:24:37
【问题描述】:
我在数组列表中有两个项目
- 位置 0:product_id=500,amout=1
- 位置 1:product_id=501,amout=1
我想创建这种类型的json
{
"user_id": "3",
"shipping_id": "1",
"payment_id": "2",
"products": {
"500": {
"product_id": "500",
"amount": "1"
},
"501": {
"product_id": "501",
"amount":"1"
}
}
}
这是我的代码
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("user_id", usr);
jsonObject.accumulate("shipping_id", shipping_id);
jsonObject.accumulate("payment_id", payment_id);
for( i=0;i<alProductss.size();i++) {
String a = alProductss.get(i).getAnount();
String b = alProductss.get(i).getProduct_id();
JSONObject productObject = new JSONObject();
jsonObject.accumulate("products", productObject);
JSONObject numberObject = new JSONObject();
numberObject.accumulate("product_id", b);
numberObject.accumulate("amount", a);
productObject.accumulate(b, numberObject);
}
我收到了这个回复:
{
"user_id":"230",
"shipping_id":"1",
"payment_id":"14",
"products":[
{"579":
{
"product_id":"579",
"amount":"1"
}
},
{"593":
{
"product_id":"593",
"amount":"1"
}
}]
}
但我想要这个 json 响应,请帮助获得结果响应。
{ "user_id": "3", "shipping_id": "1", "payment_id": "2", "products": { "500": { "product_id": "500", "amount": "1" }, "501": { "product_id": "501", "amount":"1" } } }
【问题讨论】: