使用Java啓動程序檢查遠程服務器是否有最新版本的可用軟件。服務器響應一些JSON數據,我使用gson將其轉換爲對象。將簡單的json字符串轉換爲對象,始終將屬性設置爲null
但是,數據總是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
。
我覺得我失去了一些東西很愚蠢的在這裏...
這不需要吸氣劑和吸氣劑。 gson應該正確設置屬性值。我已經使用過這種方法十幾次了,我沒有看到有任何理由不起作用。即使他們的例子沒有定義任何getter/setter。另外,當財產被公開時,獲得者/設置者是無用的 – helion3