1

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

上記のオブジェクトには、各行の最後の項目のみがあります。

4

5 に答える 5

2

あなたJSONObject json = new JSONObject();はループ内にいる必要があります。

さらに、(パフォーマンスのために) プロパティにアクセスするたびに get(i) を実行しないでください。for ループの他の構造を使用できます。

for (OrderDetailsData data : orderList) {
  JSONObject json = new JSONObject();
  json.put("orderno", data.getOrderNumber().toString());
  // ...
}

最後に、他の Web サービスでコードを再利用できるように、OrderDetailsData から JSON オブジェクトを読み書きする関数を用意することを検討する必要があります。

于 2013-08-26T11:07:25.767 に答える
1

小さな間違い

try {
            for (int i = 0; i < orderList.size(); i++) {

                JSONObject json = new JSONObject(); // update here

                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;
        }
于 2013-08-26T11:01:50.727 に答える
0

for ループ内で json オブジェクトをインスタンス化する必要があります。

于 2013-08-26T11:15:29.617 に答える