Create an array of JSON Objects

后端 未结 5 1994
滥情空心
滥情空心 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:16

    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;
    }
    
    0 讨论(0)
  • 2021-01-07 10:18

    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);
    }
    
    0 讨论(0)
  • 2021-01-07 10:21

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-07 10:37

    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;
            }
    
    0 讨论(0)
提交回复
热议问题