How can I delete the nameValuePairs key from the JSONObject?

前端 未结 7 2039
陌清茗
陌清茗 2020-12-05 07:07

I\'m working on an Android project which needs a JSONObject for the body of my POST request. After putting the keys and values of the JSON I got the following line:

7条回答
  •  甜味超标
    2020-12-05 07:46

    Thanks to 13KZ, pointed me in the right direction, and to flesh it out here is what I now have to solve this issue.

    Definitions

    private JsonObject gsonResultTwoWeek;
    private JsonObject gsonResultDay;
    private JsonObject gsonResult;
    

    Initialise

    gsonResult = new JsonObject();
    gsonResultDay = new JsonObject();
    gsonResultTwoWeek = new JsonObject();
    

    Use

    gsonResultDay.addProperty(epoch, value);
    

    where data is a string and value is an int in my case and is in a for loop to add multiple values

    And then to pull it all together

    gsonResult.addProperty("accounts", 2);
    gsonResult.add("todaydata", gsonResultDay);
    gsonResult.add("2weekdata", gsonResultTwoWeek);
    

    Finally my interface

    public interface ApiInterface {
    
        @POST("/groupdata")
        void postGroupData(@Body JsonObject body,Callback cb);
    
    }
    

    What hits my server is this

    {"accounts":2,"todaydata":{"1423814400":89,"1423816200":150,"1423818000":441},"2weekdata":{"1423699200":4869,"1423785600":1011}}
    

提交回复
热议问题