首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Realm和预期的BEGIN_OBJECT,但其编号位于路径$[0]。location.coordinates[0]

Realm和预期的BEGIN_OBJECT,但其编号位于路径$[0]。location.coordinates[0]
EN

Stack Overflow用户
提问于 2017-07-31 02:56:29
回答 1查看 647关注 0票数 0

我正在尝试使用Realm-java来存储coordnates (双精度数组),但是我无法做到。

下面是我正在尝试解析的json示例:

代码语言:javascript
复制
{"_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"
}

我的全局目标代码是这样的

代码语言:javascript
复制
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;
}

位置是类型和realmlist的组合

Location.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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,但它是路径$.location.coordinates上的数字,但我不能找出为什么这个数字不是RealmDoubleObject的“拟合”。

对于那些不熟悉realm的人,RealmList无法工作,您必须构建自己的realm对象。

谢谢。我希望在这里能找到一些领域专家!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-31 04:42:48

解决方案:使用Gson反序列化程序可以做到

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

代码语言:javascript
复制
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();

然后我们必须把一些其他的构造函数方法

代码语言:javascript
复制
public RealmDoubleObject(double v) {
    this.value = v;
}

这就是全部。

感谢@EpicPandaForce的帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45403519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档