java - Realm 和预期的 BEGIN_OBJECT,但在路径 $[0].location.coordinates[0] 处为 NUMBER

标签 java android json gson realm

我正在尝试使用 Realm-java 存储坐标( double 组),但我无法做到这一点。

这是我尝试解析的 json 示例:

{"_id":"597cd98b3af0b6315576d717",
"comarca":"string",
"font":null,
"imatge":"string",
"location":{
           "coordinates":[41.64642,1.1393],
           "type":"Point"
},
"marca":"string",
"municipi":"string",
"publisher":"string",
"recursurl":"string",
"tematica":"string",
"titol":"string"
}

我的全局对象代码是这样的

public class Images  extends RealmObject implements Serializable {
@PrimaryKey
private String _id;
private String recursurl;
private String titol;
private String municipi;
private String comarca;
private String marca;
private String imatge;
@Nullable
private Location location;
private String tematica;
private String font;
private String parentRoute;

public Location getLocation() {return location;}

public void setLocation(Location location) {this.location = location;}

public String getParentRoute() {
    return parentRoute;
}

public void setParentRoute(String parentRoute) {
    this.parentRoute = parentRoute;
}

public String get_id() {
    return _id;
}

public void set_id(String _id) {
    this._id = _id;
}

public String getFont() {
    return font;
}

public void setFont(String font) {
    this.font = font;
}

public String getRecursurl() {
    return recursurl;
}

public void setRecursurl(String recursurl) {
    this.recursurl = recursurl;
}

public String getTitol() {
    return titol;
}

public void setTitol(String titol) {
    this.titol = titol;
}

public String getMunicipi() {
    return municipi;
}

public void setMunicipi(String municipi) {
    this.municipi = municipi;
}

public String getComarca() {
    return comarca;
}

public void setComarca(String comarca) {
    this.comarca = comarca;
}

public String getMarca() {
    return marca;
}

public void setMarca(String marca) {
    this.marca = marca;
}

public String getImatge() {
    return imatge;
}

public void setImatge(String imatge) {
    this.imatge = imatge;
}


public String getTematica() {
    return tematica;
}

public void setTematica(String tematica) {
    this.tematica = tematica;
}

位置是类型和 Realm 列表的组合

位置.java

public class Location  extends RealmObject implements Serializable {
private String type;
private RealmList<RealmDoubleObject> coordinates;


public Location() {
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public RealmList<RealmDoubleObject> getCoordinates() {
    return coordinates;
}

public void setCoordinates(RealmList<RealmDoubleObject> coordinates) {
    this.coordinates = coordinates;
}

}

RealmDoubleObject.java

public class RealmDoubleObject extends RealmObject implements Serializable{
private Double value;

public RealmDoubleObject() {
}

public Double getDoublevalue() {
    return value;
}

public void setDoublevalue(Double value) {
    this.value = value;
}

}

错误是 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at path $[0].location.coordinates[0] 但我无法弄清楚为什么是这个数字RealmDoubleObject 不“适合”。

对于那些不熟悉 Realm 的人来说,RealmList 不起作用,您必须构建自己的 Realm 对象。

谢谢。希望在这里能找到一些 Realm 高手!

最佳答案

已解决: 使用Gson反序列化器可以做到

首先我们必须像这样初始化 gson 对象

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(new TypeToken<RealmList<RealmDoubleObject>>() {}.getType(), new TypeAdapter<RealmList<RealmDoubleObject>>() {

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

                @Override
                public RealmList<RealmDoubleObject> read(JsonReader in) throws IOException {
                    RealmList<RealmDoubleObject> list = new RealmList<RealmDoubleObject>();
                    in.beginArray();
                    while (in.hasNext()) {
                        Double valor = in.nextDouble();
                        list.add(new RealmDoubleObject(valor));
                    }
                    in.endArray();
                    return list;
                }
            })
            .create();

然后我们必须添加一些其他构造函数

public RealmDoubleObject(double v) {
    this.value = v;
}

这就是全部。

感谢@EpicPandaForce的帮助

关于java - Realm 和预期的 BEGIN_OBJECT,但在路径 $[0].location.coordinates[0] 处为 NUMBER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45403519/

相关文章:

Android 5.0 - Intellij Gradle : Cannot resolve symbol: @color:material_blue_500

mysql - 尝试在父级下获取 perl 输出 json 数据

java - CORBA 序列 : can I define sequences of objects w/methods?

java - 如何在kafka producer中切换到自定义编码器?

java - AutoCloseable 与垃圾回收的关系

java - 当我尝试创建 ListActivity 来访问其他 Activity 时,它只显示 "Unfortunately, ReptileKeeper has stopped"

android - 如何为 fragment 交易添加标签?

java - Jackson 中的自定义 Json 反序列化器仅适用于 Hashmap

xml - Moxy:对象列表 XML 和 JSON 不能同时看起来不错

java - 如何在 ArrayList 上编写 for 循环来查找最大值