Create an array of JSON Objects

后端 未结 5 1997
滥情空心
滥情空心 2021-01-07 09:37

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

public boolean submitOrder(ArrayList orderList) {

        serUri          


        
5条回答
  •  执念已碎
    2021-01-07 10:34

    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.

提交回复
热议问题