【问题标题】:Parsing Integer primitive types in Realm在 Realm 中解析整数原始类型
【发布时间】:2016-08-22 15:21:43
【问题描述】:

想要在 Realm 中解析这个 JSON 响应,但它总是在类别中崩溃:

[
  {
    "id": 32,
    "name": "ABC",
    "height": "49.5000",
    "categories": [
      14,15,16
    ]
  }
]

Info.java:

public class Info extends RealmObject {

    @PrimaryKey
    private Integer id;
    private String name;
    private Integer height;
    private RealmList<RealmInt> categories;
}

RealmInt.java

public class RealmInt extends RealmObject{
    private Integer val;

    public RealmInt() {

    }

    public RealmInt(Integer val) {
        this.val = val;
    }

    public Integer getVal() {
        return val;
    }
}

这是我收到这个 JSON 时的解析方式:

String stringBody = response.body().string();
List<Info> newObjects = GsonIntWrapper.intBuilder().fromJson(stringBody, new TypeToken<List<Info>>(){}.getType());
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(newObjects);

GsonIntWrapper.intBuilder()

公共类 GsonIntWrapper {

public static Gson intBuilder(){
    Type tokenInt = new TypeToken<RealmList<RealmInt>>(){}.getType();

    Gson gson = new GsonBuilder()
            .setExclusionStrategies(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(FieldAttributes f) {
                    return f.getDeclaringClass().equals(RealmObject.class);
                }

                @Override
                public boolean shouldSkipClass(Class<?> clazz) {
                    return false;
                }
            })
            .registerTypeAdapter(tokenInt, new TypeAdapter<RealmList<RealmInt>>() {

                @Override
                public void write(JsonWriter out, RealmList<RealmInt> value) throws IOException {
                    // Ignore
                }

                @Override
                public RealmList<RealmInt> read(JsonReader in) throws IOException {
                    RealmList<RealmInt> list = new RealmList<RealmInt>();
                    in.beginArray();
                    while (in.hasNext()) {
                        list.add(new RealmInt(in.nextInt()));
                    }
                    in.endArray();
                    return list;
                }
            })
            .create();
    return gson;
  }
}

崩溃日志:

Caused by: java.lang.NumberFormatException: Expected an int but was 49.5000 at line 1 column 18581 path $[4].height

【问题讨论】:

  • 请添加崩溃日志
  • 嘿@Tim,刚刚在问题底部添加了有意义的崩溃行。感谢您的关注。
  • 如果要将其值设置为49.5000,为什么heightInteger
  • 在我看来很简单,你有一个 double/float,它需要一个 int
  • @Hey Micheal,高度是一个数字,如果我错了,请纠正我,对于 Realm,我们没有浮点数或小数,但我相信我们使用通用类型 Integer??

标签: android realm realm-list


【解决方案1】:

您的日志说明了一切,您的 json 上的变量“height”不是 int 修改模型如下:

public class Info extends RealmObject {

    @PrimaryKey
    private Integer id;
    private String name;
    private Double height;
    private RealmList<RealmInt> categories;
}

【讨论】:

  • 完美运行,这是我的错 :) 感谢您的支持!
【解决方案2】:

在您的 Json 值中,“高度”是 float,但在您的 RealmObject 类 (Info) 中是 int

你可以看到这个链接这是一个类似的问题click here

【讨论】:

  • 谢谢你的回答意思是一样的。
猜你喜欢
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多