Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 解析域中的整数基元类型_Android_Realm_Realm List - Fatal编程技术网

Android 解析域中的整数基元类型

Android 解析域中的整数基元类型,android,realm,realm-list,Android,Realm,Realm List,想要在领域中解析此JSON响应,但对于以下类别它总是崩溃: [ { "id": 32, "name": "ABC", "height": "49.5000", "categories": [ 14,15,16 ] } ] Info.java: 当我收到这个JSON时,我就是这样解析的: 在Json中,值“height”是float,但在RealmObject类中(Info)是int 你可以看到这个链接,这是一个类似的问题你的日志说明了

想要在领域中解析此JSON响应,但对于以下类别它总是崩溃:

[
  {
    "id": 32,
    "name": "ABC",
    "height": "49.5000",
    "categories": [
      14,15,16
    ]
  }
]
Info.java: 当我收到这个JSON时,我就是这样解析的: 在Json中,值“height”是float,但在RealmObject类中(Info)是int


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

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

public class Info extends RealmObject {

    @PrimaryKey
    private Integer id;
    private String name;
    private Double height;
    private RealmList<RealmInt> categories;
}
public类信息扩展RealmObject{
@主键
私有整数id;
私有字符串名称;
私人双高;
私人地产类别;
}

请添加崩溃日志hey@Tim,在问题的底部添加有意义的崩溃行。感谢您的关注。为什么
height
是一个
Integer
如果您想将其值设置为
49.5000
?在我看来很简单,在需要int@HeyMicheal身高是个数字如果我错了请纠正我,对于Realm,我们没有浮点数或十进制数,但我相信,我们使用的是泛型整型??工作得很好,这是我的错:)感谢您的支持!谢谢,因为你的回答也有同样的意思。
public class RealmInt extends RealmObject{
    private Integer val;

    public RealmInt() {

    }

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

    public Integer getVal() {
        return val;
    }
}
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);
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
public class Info extends RealmObject {

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