Create an array of JSON Objects

后端 未结 5 1998
滥情空心
滥情空心 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: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 orderList) {
        Gson gson = new Gson();
        JsonObject myObj = new JsonObject();
    
        JsonElement ordersObj = gson.toJsonTree(orderList);
        myObj.add("list", ordersObj);
    }
    

提交回复
热议问题