Gson deserializes Boolean values from 1 to false in Java and Android
The courses I am using are as follows:
public class TheJob {
private String jobDescription = null, jobAdditionalInfo = null, jobAddress = null;
@SerializedName("jobActive")
public boolean jobActive = true;
@SerializedName("offsiteAllowed")
public boolean offsiteAllowed;
}
The JSON I received is as follows:
[{"jobId":"2","jobDescription":"Beta","jobAdditionalInfo":"Edited ","jobAddress":"103 Emus Avenue \nCenturion \n0157 \nSouth Africa \n","jobActive":"1","offsiteAllowed":"1"}]
I deleted the remaining JSON array entries because they are identical
I can't get the Boolean fields jobactive and offsiteallowed to parse correctly. Even if the JSON value is 1, they always resolve to false
With the exception of Boolean values, everything else in the job class is completely deserialized
Any suggestions would be appreciated
resolvent:
Gson pays great attention to types. To resolve int values to Boolean values, you may have to register a custom Boolean deserialization class. Alternatively, if you control API changes, it returns true instead of 1. It expects "true", And treat everything else as false. There should be some examples of registering a custom jsondeserializer class. The adapter allows you to perform operations such as deserializing a boolean variable named isfruit and returning apple, orange, carrot, etc. from the API. After registration, you can implement a custom deserializer to evaluate the value and deserialize true and false as isfruit as needed