Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带键的Gson字符串_Java_Android_Json_Serialization_Gson - Fatal编程技术网

Java 带键的Gson字符串

Java 带键的Gson字符串,java,android,json,serialization,gson,Java,Android,Json,Serialization,Gson,我使用Gson将对象转换为json字符串,工作正常,但是当我使用post将json发送到webservice方法时,我必须在字符串中添加post方法的参数名 例如: 我从Gsonnew Gson()获得的json字符串。toJson(requestDataDTO): { "req": { "AppId": "2", "ThirdParty": "3", "UserId": "1", "UserToken": "4" }, "req1": { "

我使用Gson将对象转换为json字符串,工作正常,但是当我使用post将json发送到webservice方法时,我必须在字符串中添加post方法的参数名

例如:

我从Gson
new Gson()获得的json字符串。toJson(requestDataDTO)

{
  "req": {
    "AppId": "2",
    "ThirdParty": "3",
    "UserId": "1",
    "UserToken": "4"
  },
  "req1": {
    "AppId": "-33",
    "ThirdParty": "3",
    "UserId": "1",
    "UserToken": "4"
  }
}
jsonString我想要:

{
  "requestDataDTO": {
    "req": {
      "AppId": "2",
      "ThirdParty": "3",
      "UserId": "1",
      "UserToken": "4"
    },
    "req1": {
      "AppId": "-33",
      "ThirdParty": "3",
      "UserId": "1",
      "UserToken": "4"
    }
  }
}
现在,我在从Gson获得的json字符串的开头添加了这个
“requestDataDTO”
字符串


有没有办法做到这一点?

假设您有一个看起来像这样的对象:

package com.dominikangerer.q25077756;

import com.google.gson.annotations.SerializedName;

public class RequestDataDTO {
    // {"AppId":"2","ThirdParty":"3","UserId":"1","UserToken":"4"}

    @SerializedName("AppId")
    private String appId;
    @SerializedName("ThirdParty")
    private String thirdParty;
    @SerializedName("UserId")
    private String userId;
    @SerializedName("UserToken")
    private String userToken;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getThirdParty() {
        return thirdParty;
    }

    public void setThirdParty(String thirdParty) {
        this.thirdParty = thirdParty;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserToken() {
        return userToken;
    }

    public void setUserToken(String userToken) {
        this.userToken = userToken;
    }

}
package com.dominikangerer.q25077756;

import java.util.HashMap;

public class RequestDataDTOContainer {

    private HashMap<String, RequestDataDTO> requestDataDTO = new HashMap<String, RequestDataDTO>();

    public HashMap<String, RequestDataDTO> getRequestDataDTO() {
        return requestDataDTO;
    }

    public void setRequestDataDTO(HashMap<String, RequestDataDTO> requestDataDTO) {
        this.requestDataDTO = requestDataDTO;
    }

    public void putRequestDataDTO(String key, RequestDataDTO value){
        this.requestDataDTO.put(key, value);
    }

}
// enable pretty printing
Gson gson = new GsonBuilder().setPrettyPrinting().create();

// too lazy to fill the objects by hand
String reqJson = "{\"AppId\":\"2\",\"ThirdParty\":\"3\",\"UserId\":\"1\",\"UserToken\":\"4\"}";
String req1Json = "{\"AppId\":\"-33\",\"ThirdParty\":\"3\",\"UserId\":\"1\",\"UserToken\":\"4\"}";

// deserialize it with gson
RequestDataDTO req = gson.fromJson(reqJson, RequestDataDTO.class);
RequestDataDTO req1 = gson.fromJson(req1Json, RequestDataDTO.class);

// initiliaze the container
RequestDataDTOContainer container = new RequestDataDTOContainer();

// adding the 2 req objects with the certain key
container.putRequestDataDTO("req", req);
container.putRequestDataDTO("req1", req1);

// Print it as pretty json
System.out.println(gson.toJson(container));
最简单也是对我来说最易读的解决方案是创建一个包装器/容器类,其中包含一个HashMap(键/值),如下所示:

package com.dominikangerer.q25077756;

import com.google.gson.annotations.SerializedName;

public class RequestDataDTO {
    // {"AppId":"2","ThirdParty":"3","UserId":"1","UserToken":"4"}

    @SerializedName("AppId")
    private String appId;
    @SerializedName("ThirdParty")
    private String thirdParty;
    @SerializedName("UserId")
    private String userId;
    @SerializedName("UserToken")
    private String userToken;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getThirdParty() {
        return thirdParty;
    }

    public void setThirdParty(String thirdParty) {
        this.thirdParty = thirdParty;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserToken() {
        return userToken;
    }

    public void setUserToken(String userToken) {
        this.userToken = userToken;
    }

}
package com.dominikangerer.q25077756;

import java.util.HashMap;

public class RequestDataDTOContainer {

    private HashMap<String, RequestDataDTO> requestDataDTO = new HashMap<String, RequestDataDTO>();

    public HashMap<String, RequestDataDTO> getRequestDataDTO() {
        return requestDataDTO;
    }

    public void setRequestDataDTO(HashMap<String, RequestDataDTO> requestDataDTO) {
        this.requestDataDTO = requestDataDTO;
    }

    public void putRequestDataDTO(String key, RequestDataDTO value){
        this.requestDataDTO.put(key, value);
    }

}
// enable pretty printing
Gson gson = new GsonBuilder().setPrettyPrinting().create();

// too lazy to fill the objects by hand
String reqJson = "{\"AppId\":\"2\",\"ThirdParty\":\"3\",\"UserId\":\"1\",\"UserToken\":\"4\"}";
String req1Json = "{\"AppId\":\"-33\",\"ThirdParty\":\"3\",\"UserId\":\"1\",\"UserToken\":\"4\"}";

// deserialize it with gson
RequestDataDTO req = gson.fromJson(reqJson, RequestDataDTO.class);
RequestDataDTO req1 = gson.fromJson(req1Json, RequestDataDTO.class);

// initiliaze the container
RequestDataDTOContainer container = new RequestDataDTOContainer();

// adding the 2 req objects with the certain key
container.putRequestDataDTO("req", req);
container.putRequestDataDTO("req1", req1);

// Print it as pretty json
System.out.println(gson.toJson(container));
如果您想添加更多的元信息,如整个元对象或类似的元对象,而不向json添加硬编码字符串,那么您现在就更加灵活了


您可以在这个github存储库中找到整个示例:

Hi Rohit。你需要更多的信息吗?:)不你先前的回答帮助了我。让我接受这一点。谢谢:)