Gson布尔值转换始终为false

时间:2015-07-16 15:10:33

标签: android json gson retrofit

我使用Gson作为转换器通过Retrofit来解析我的回复,但即使回应正确,我也总是变得虚假。

它是这样的

响应:

{"groups":{"success":true}}

GroupsResponseClass

public class GroupsResponse {

    @SerializedName("success")
    private boolean mSuccess;

    GroupsResponse() {}

    public boolean isSuccess() {
        return mSuccess;
    }

    public void setSuccess(boolean success) {
        mSuccess = success;
    }
}

我停止使用GsonConverter并仍然获得一个成功虚假值的对象

1 个答案:

答案 0 :(得分:6)

您的groups对象在技术上嵌套在另一个对象中,可能需要展平您的json。如果不能,这应该有效:

GroupsResponse.java

public class GroupsResponse {

    @SerializedName("groups")
    private Groups mGroups;

    GroupsResponse() {}

    public boolean isSuccess() {
        return mGroups.isSuccess();
    }

    public void setSuccess(boolean success) {
        mGroups.setSuccess(success);
    }
}

Groups.java

public class Groups {

    @SerializedName("success")
    private boolean mSuccess;

    Groups() {}

    public boolean isSuccess() {
        return mSuccess;
    }

    public void setSuccess(boolean success) {
        mSuccess = success;
    }
}