正在开发一个 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/