领域和预期BEGIN_OBJECT但在路径$ [0]处为NUMBER .location.coordinates [0]

时间:2017-07-30 18:56:29

标签: java android json gson realm

我试图使用Realm-java存储coordnates(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;
}

位置是类型和现实主义者的复合

Location.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:预期BEGIN_OBJECT但在路径$ [0] .location.coordinates [0]处是NUMBER但我无法弄清楚原因这个数字不是&#34;拟合&#34;通过RealmDoubleObject。

对于那些不熟悉领域的人来说,RealmList不起作用,你必须建立自己的领域对象。

谢谢。我希望在这里找到一些Realm专家!

1 个答案:

答案 0 :(得分:1)

解决: 使用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的帮助