java - Gson string with key -


I am using GSO to convert an object to Jason string, and its work is fine but when I I'm sending Jason to the webservice method using the post service, I have to add the parameter name of the post method in the string.

Example:

jsonString I'm getting new GSN from GSON ToJson (requestDataDTO) :

  {"Req1": {"AppId": "" 33 "," UserId ":" 1 "," UserToken ":" 4 "} , "Req1": {"AppId": "-33" "3", "UserId": "1", "UserToken": "4"}}  

jsonString I want to: "" "", "User ID": "1", "usercut": "4"}, "rake 1": {"request": {"request": {"blank": {"APID": "2", "third party": {3} "APIID": "-33", "thirdparty": "3", "user-id": "1", "usercuton": "4"}} }

For now I am joining GSN by adding this "requestDataDTO" string at the beginning of the GSN string.

What to gain Is there a way?

Assume that you have an object in some way:

  package com.dominikangerer.q25077756; Import com.google.gson.annotations.SerializedName; Public Class RequestDataDTO {// {"AppId": "2", "Third Party": "3", "Users ID ":" 1 "," UserToken ":" 4 "} @ Serialized Names (" APPID ") Private string app ID; @ SerializedName ("third party") private string third party; @ SerializedName ("UserId") private string userId; @ SerializedName ("UserToken") private string userToken; Public string getAppId () {return appId; } Public Zero setAppId (String API) {this.appId = appId; } Public string getThirdParty () {return third party; } Public Zero SetredParty (string third party) {this.thirdParty = thirdparty; } Public string getUserId () {return userId; } Public Zero SetUserID (String UserID) {this.userId = userId; } Public string getUserToken () {userToken; } Public Zero SetUseToken (String UserTouch) {this.userToken = userToken; The easiest and most readable solution for me is to create a cover / container class in which the Hashmap (key / value) is like this:  
  package com.dominikangerer.q25077756; Import java.util.HashMap; Public class requests data loss protocol String, request.datio & gt; RequestDataDTO = new hashmap & lt; String, request.datio & gt; (); Public Hashmap & lt; String, request.datio & gt; GetRequestDataDTO () {Refund Request DataDTO; } Public Zero SetRequestedTatio (Hashmap & lt; String, RequestDateTo & request; RequestDATTOO) {this.requestDataDTO = requestDataDTO; } Public Zero putRequestDataDTO (string key, RequestDataDTO value) {this.requestDataDTO.put (key, value); }}  

To test it, just test it with a key like this:

  // Enable Enable Printing Gson gson = new Gsonbuilder (). SetPrettyPrinting) .making (); // The very sluggish string to fill the objects by hand is Recijean = "{\" AppId \ ": \" 2 \ ", \" third party \ ": \" 3 \ ", \" UserId \ ": \" 1 \ ", \" UserToken \ ": \" 4 \ "}"; String rake 1 jason = "{\" AppId \ ": \" - 33 \ ", \" third party \ ": \" 3 \ ", \" UserId \ ": \" 1 \ ", \" UserToken \ " : \ "4 \"} "; Deserialize it with // GSN Request DataDTO REQ = gson.fromJson (reqJson, RequestDataDTO.class); RequestDataDTO req1 = gson.from Jason (req1Json, RequestDataDTO.class); // Container requestDataDataCountainEye container = new requestDataDataCentre (); // Add 2 Ruck Objects with some major containers. InputquestattiTTO ("rake", rake); Container.putquestionateto ("rake 1", rake 1); // Print it as a beautiful json system.out.println (gson.toJson (container));  

You now have more flexibility if you want to add more meta information such as whole meta object or similar jason without adding hardcode string.

You can find the complete example in this glyph repository:


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -