GSON不解析布尔值(始终为false) [英] GSON not parsing booleans (always false)

查看:866
本文介绍了GSON不解析布尔值(始终为false)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Retrofit来获取JSON文档.问题是,所有布尔值始终为假.

I am using Retrofit in order to get a JSON document. Problem is, all booleans are always false.

响应如下:

{
   "gender":[0,1],
   "age":[20,30],
   "likesLeaveHome":false,
   "likesSport":false,
   "likesCulture":false,
   "likesTraveling":false
   ...
}

我正在用

onResponse(Call<SearchProfile> call, Response<SearchProfile> response)

响应应解析为的SearchProfile类如下所示:

And the class of SearchProfile which the response should be parsed to looks like that:

public class SearchProfile {

    public ArrayList<Integer> gender = new ArrayList<>(); // works fine
    public ArrayList<Integer> age    = new ArrayList<>(); // works fine

    ...

    public Boolean likesLeaveHome = true;    // always false

    @SerializedName("likesSport")
    public boolean likesSport     = true;    // always false

    @SerializedName("likesCulture")
    public Boolean likesCulture;             // always false

    @SerializedName("likesTraveling")
    public Boolean mLikesTraveling;          // always false

    public boolean isLikesTraveling() {
        return mLikesTraveling;
    }

    public void setLikesTraveling(boolean likesTraveling) {
        mLikesTraveling = likesTraveling;
    }

}

如您所见,这是一个简单的pojo类.像性别"和年龄"这样的列表可以很好地工作.尽管如此,布尔值仍然无法设置. (这很奇怪,因为通过Retrofit发送此对象正是发送了此文档,因此GSON肯定知道布尔值).

As you can see, it is a simple pojo class. Lists like "gender" and "age" work perfectly fine. Still, the booleans can't be set. (This is especially strange since sending this object via Retrofit sends exactly this document so GSON surely knows booleans).

如片断中所示,我还尝试了其他方法,例如将 Boolean 包装器类指定为类型,而不是boolean. 我还使用了 @SerializeName 批注或getter和setts方法等. 仍然所有布尔值始终都是错误的.即使我将它们的默认值声明为true(所以GSON似乎总是用false覆盖此值).

As shown in the snipped, I also tried other methods like giving the Boolean wrapper class as type instead of boolean. I also used a @SerializeName annotation or getter and setts methods etc. Still all booleans are always false. Even if I declare them default as true (so GSON always seems to overwrite this value with false).

希望有人有个好主意!

推荐答案

示例中要解析的JSON的所有值都为false.解析时,GSON将使用反射来覆盖类的字段值,这意味着从JSON解析的任何值都将是该字段的值,而不管它如何在类中初始化.

The JSON you are parsing in your example has all the values as false. When parsing, GSON will use reflection to overwrite field values for a class, meaning that whatever value is parsed from JSON will be the value of the field, regardless of how it is initialized in the class.

这是一个示例响应,它将导致将字段设置为true.同样,这取决于您要解析的JSON.

Here is a sample response that will cause the fields to be set to true. Again, it's just up to whatever JSON you are parsing.

{
   "gender":[0,1],
   "age":[20,30],
   "likesLeaveHome":true,
   "likesSport":true,
   "likesCulture":true,
   "likesTraveling":true
   ...
}

这篇关于GSON不解析布尔值(始终为false)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆