The JSON string is {"status":0}
or {"status":300}
and the Java class
public class MyResponse {
private Integer status;
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "MyResponse{" +"status=" + status +'}';
}
}
When status field is an Integer it is converted to null for all incoming values. When it is of type int only 0 is deserialized correctly. Other values are converted to null.
What am I missing?
Here is how I am trying to deserialize
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setPrettyPrinting().create();
gson.fromJson(json.toString(), MyResponse.class);
You need to remove the setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.
If you leave it, then it expects the field in your class to be named Status
and not status
as stated in the documention : someFieldName ---> SomeFieldName
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.