问题描述:

Working on a java launcher that checks a remote server for the latest version of software available. The server responds with some JSON data, which I'm using gson to convert into an object.

However, the data is always null and I can't figure out why. I've done this a dozen times without a problem.

The class gson will use:

public class VersionResponse {

public String currentVersion;

}

The code. I've taken a sample json string to avoid any problems with the remote server file. It's valid json, I've verified.

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);

}

The object's currentVersion property is always showing null.

I feel like I'm missing something really dumb here...

网友答案:
public class VersionResponse {
    public String currentVersion;

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

    public String getCurrentVersion(){
       return this.currentVersion;
    } }
网友答案:

Found the problem... the obfuscation we have on the build process was breaking the gson's ability to store the values properly.

网友答案:

your VersionResponse class missing getter and setter

public class VersionResponse { public String currentVersion;

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

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

}

相关阅读:
Top