将简单的json字符串转换为对象始终将属性设置为null

时间:2013-11-15 01:22:03

标签: java json

使用java启动程序检查远程服务器上是否有可用的最新软件版本。服务器响应一些JSON数据,我使用gson将其转换为对象。

但是,数据总是null,我无法弄清楚原因。我已经做了十几次没有问题。

gson类将使用:

public class VersionResponse {
    public String currentVersion;
}

代码。我已经采用了一个示例json字符串,以避免远程服务器文件出现任何问题。这是有效的json,我已经验证了。

Gson gson = new GsonBuilder().create();
remoteVersionResponse = gson.fromJson("{\"currentVersion\":\"v.0.0.1-96-g48c1f4d\"}", VersionResponse.class);

if( remoteVersionResponse != null ){
    System.out.println("Object: " + remoteVersionResponse.currentVersion);
}

对象的currentVersion属性始终显示null

我觉得我错过了一些非常愚蠢的东西......

3 个答案:

答案 0 :(得分:0)

public class VersionResponse {
    public String currentVersion;

    public void setCurrentVersion(String version){
        this.currentVersion = version;
    }  

    public String getCurrentVersion(){
       return this.currentVersion;
    } }

答案 1 :(得分:0)

发现问题......我们对构建过程的混淆破坏了gson正确存储值的能力。

答案 2 :(得分:-1)

你的VersionResponse类缺少getter和setter

public class VersionResponse {     public String currentVersion;

public void setCurrentVersion(String version){
    this.currentVersion = version;
}  

public String getCurrentVersion(){
    return this.currentVersion;
}

}