Gaurav
Gaurav

Reputation: 535

Create an array of JSON Objects

I need to create a JSON Object for an Arraylist. Below is the code

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

The problem is its creating object with details of last row item at each position. Say if my orderList has 3 rows, the jsonObject created has 3 rows but with 3rd row data in all 3. Seems like its overriding data for all the rows with latest row fetched. I tried with couple of other ways but still not getting desired result. Please advise. Thanks.

JSON Object created:

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

The above object has only last item in each row.

Upvotes: 1

Views: 4325

Answers (5)

Purushotham
Purushotham

Reputation: 3820

This following snippet will create array of json objects in single statement, it even performs null checks while creating json from object using Google's Gson library.

public boolean submitOrder(ArrayList<OrderDetailsData> orderList) {
    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    JsonElement ordersObj = gson.toJsonTree(orderList);
    myObj.add("list", ordersObj);
}

Upvotes: 1

LOG_TAG
LOG_TAG

Reputation: 20569

Look at this I'm trying to use GSON lib, try out this, I'm not tested. This should work fine.

From Json string to Json object this way:

String jsonStr = "{\"a\": \"A\"}";

Gson gson = new Gson();
JsonElement element = gson.fromJson (jsonStr, JsonElement.class);
JsonObject jsonObj = element.getAsJsonObject();

For your code this should work fine:

public boolean submitOrder(ArrayList<OrderDetailsData> orderList) 
{
    serUri = "lists.json";
    method = "post";

    Gson gson = new Gson();
    String jsonstr = new Gson().toJson(orderList);
    JsonElement element = gson.fromJson (jsonStr, JsonElement.class);
    JsonObject jsonObject = element.getAsJsonObject();

    WebServiceAsyncTask webServiceTask = new WebServiceAsyncTask();
    webServiceTask.execute(serUri, method,jsonObject, this);
    return true;
}

Upvotes: 1

sharma_arjun
sharma_arjun

Reputation: 1

You need to instantiate the json object inside the for loop.

Upvotes: 0

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Your JSONObject json = new JSONObject(); should be within the loop.

Additionally, you should not do a get(i) each time to access properties (for performance). You can use the other construct of the for loop:

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

Finally, maybe you should consider having a function that reads/writes a JSON object from an OrderDetailsData so that you can reuse the code in other webservices.

Upvotes: 2

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Small mistake

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

Upvotes: 1

Related Questions