我需要为 Arraylist 创建一个 JSON 对象。下面是代码
public boolean submitOrder(ArrayList<OrderDetailsData> orderList) {
serUri = "lists.json";
method = "post";
JSONObject jsonObject = new JSONObject();
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
try {
for (int i = 0; i < orderList.size(); i++) {
json.put("orderno", orderList.get(i).getOrderNumber()
.toString());
json.put("tableno", orderList.get(i).getTableNumber()
.toString());
json.put("itemname", orderList.get(i).getItemName().toString());
json.put("amount", orderList.get(i).getAmount().toString());
json.put("ordstatus", orderList.get(i).getOrderStatus()
.toString());
array.put(json);
}
catch (JSONException je) {
return false;
}
try {
jsonObject.put("list", array);
} catch (JSONException je) {
return false;
}
WebServiceAsyncTask webServiceTask = new WebServiceAsyncTask();
webServiceTask.execute(serUri, method,jsonObject, this);
return true;
}
问题在于它创建的对象包含每个位置的最后一行项目的详细信息。假设我的 orderList 有 3 行,则创建的 jsonObject 有 3 行,但在所有 3 行中都有第 3 行数据。似乎它是所有获取最新行的行的覆盖数据。我尝试了其他几种方法,但仍然没有得到想要的结果。请指教。谢谢。
创建的 JSON 对象:
{"list":[{"amount":"10.50","orderno":"0220130826163623","quantity":"1","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"},{"amount":"10.50","orderno":"0220130826163623","itemname":"Pollo Al Horno","tableno":"02","ordstatus":"placed"}]}
上面的对象在每一行中只有最后一项。