Create an array of JSON Objects

后端 未结 5 2022
滥情空心
滥情空心 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 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;
    }
    

提交回复
热议问题