How can I delete the nameValuePairs key from the JSONObject?

前端 未结 7 2030
陌清茗
陌清茗 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<StatusResponse> cb);
    
    }
    

    What hits my server is this

    {"accounts":2,"todaydata":{"1423814400":89,"1423816200":150,"1423818000":441},"2weekdata":{"1423699200":4869,"1423785600":1011}}
    
    0 讨论(0)
  • 2020-12-05 07:48

    It appears that you are attempting to transmit the actual JSONObject rather the the JSON text-string representation of the object. A look at the specification for the JSONObject class shows that you should be using the .toString() method to get the JSON text representation of the data structure kept by the JSONObject. Thus, you should be able to change:

    public interface MyService {
        @Headers({"Content-type: application/json",
                  "Accept: */*"})
        @POST("/test")
        void testFunction(@Body JSONObject jsonObject, Callback<Response> callback);
    }
    

    to:

    public interface MyService {
        @Headers({"Content-type: application/json",
                  "Accept: */*"})
        @POST("/test")
        void testFunction(@Body String jsonObject.toString(), Callback<Response> callback);
    }
    

    The only change being JSONObject jsonObject to String jsonObject.toString().

    Alternately, you could brute force it by just taking the string that you have have of the JSON and replace '"name_value_pairs": {' with '' and the last '}' in the string with ''. JSON is just a string of text. Other than it being inelegant, there is no reason that you can not manipulate the text. Those two replacements will result in a valid JSON text-object. The whitespace indentation won't look correct to a human, but a machine parsing the JSON string does not care if the whitespace is correct.

    0 讨论(0)
  • 2020-12-05 07:49

    Issue:

    Retrofit by default uses GSON to convert HTTP bodies to and from JSON. The object which is specified with @Body annotation will be passed to GSON for serialization, which basically converts the JAVA object to JSON representation. This JSON representation will be the HTTP request body.

    JSONObject stores all the key-value mapping in a member variable by name nameValuePairs. Here is an excerpt of JSONObject implementation:

    public class JSONObject {
        ...
        private final Map<String, Object> nameValuePairs;
        ...
    }
    

    When you pass JSONObject to @Body annotation, this JSONObject is seraliazed, hence the HTTP request body contains : {"nameValuePairs": "actual JSON Object"}.

    Solution:

    Pass the actual JAVA object to @Body annotation, not it's corresponding JSONObject. GSON will take care of converting it to JSON representation.

    For e.g.

    class HTTPRequestBody {
       String key1 = "value1";
       String key2 = "value2";
       ...
    }
    
    // GSON will serialize it as {"key1": "value1", "key2": "value2"}, 
    // which will be become HTTP request body.
    
    public interface MyService {
        @Headers({"Content-type: application/json",
                  "Accept: */*"})
        @POST("/test")
        void postJson(@Body HTTPRequestBody body, Callback<Response> callback);
    }
    
    // Usage
    MyService myService = restAdapter.create(MyService.class);
    myService.postJson(new HTTPRequestBody(), callback);
    

    Alternative solution:

    If you still want to send raw JSON as HTTP request body, then follow the solution mentioned by Retrofit author here.

    One of the suggested solution is to use TypedInput:

    public interface MyService {
      @POST("/test")
      void postRawJson(@Body TypedInput body, Callback<Response> callback);
    }
    
    String json = jsonRequest.toString();
    TypedInput in = new TypedByteArray("application/json", json.getBytes("UTF-8"));
    myService.postRawJson(in, callback);
    
    0 讨论(0)
  • 2020-12-05 08:00

    Use com.google.gson.JsonObject instead of org.json.JSONObject.

    JSONObject jsonRequest = new JSONObject();
    jsonRequest.put("xxxx", "zzzzzzz");
    jsonRequest.put("yyyy", "uuuuuuu");
    

    Change to

    JsonObject jsonRequest = new JsonObject();
    jsonRequest.addProperty("xxxx", "zzzzzzz");
    jsonRequest.addProperty("yyyy", "uuuuuuu");
    

    Then in interface

    public interface MyService {
        @Headers({"Content-type: application/json",
                  "Accept: */*"})
        @POST("/test")
        void testFunction(@Body JsonObject jsonObject, Callback<Response> callback);
    }
    

    JSONObject class keeping the values in LinkedHashMap with the variable name of nameValuePairs, When Gson trying to convert the JSONObject's instance into JSON, GSON keeps the structure(which has the variable nameValuePairs). That causing this problem.

    0 讨论(0)
  • 2020-12-05 08:06

    you have to covert JSONObject to JsonObject of GSON

    follow this way

     JsonParser jsonParser = new JsonParser();
      JsonObject jsonObject = (JsonObject)jsonParser.parse(actualjsonobject.toString());
    

    then pass in body

    HashMap<String,Object> body=new HashMap();
    
     body.put("content",jsonObject);
    
    0 讨论(0)
  • 2020-12-05 08:07

    My solution is based on 13KZ's

    public class MyRequest {
    
      @SerializedName(Constants.ID)
      private String myID;
      @SerializedName(Constants.PARAM_ANSWERS)
      private JsonObject answers;
    
         public MyRequest(String id, Hasmap<String, String> answers) {
             this.myID = id;
             this.answers = new JsonObject();
             for (String s: answers.keySet()) {
                 this.answers.addProperty(s, answers.get(s));
             }
         }
    }
    
    0 讨论(0)
提交回复
热议问题