使用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

我觉得我在这里想念的东西真是愚蠢...

最佳答案

public class VersionResponse {
    public String currentVersion;

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

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

关于java - 将简单的json字符串转换为对象,始终将属性设置为null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19991812/

10-10 06:06