字符串>的名单,其中GSON反序列化;进入realmList< RealmString>字符串、名单、序列化、GT

由网友(孤毒。)分享简介:我使用的是与GSON改造反序列化我的JSON到境界的对象。这都非常好,在大多数情况下。当以I'm using retrofit with gson to deserialize my json into realm objects. This works very well for the most part. Tr...

我使用的是与GSON改造反序列化我的JSON到境界的对象。这都非常好,在大多数情况下。当以

I'm using retrofit with gson to deserialize my json into realm objects. This works very well for the most part. Trouble arises when dealing with

RealmList(字符串(或任何其他基本数据类型))

RealmList(String(or any other basic data type))

由于境界犯规支持RealmList其中E犯规延伸领域对象,我在RealmObject包裹字符串。

Since Realm doesnt support RealmList where E doesnt extend Realm object, I wrapped String in a RealmObject.

public class RealmString extends RealmObject {
  private String val;

  public String getValue() {
    return val;
  }

  public void setValue(String value) {
    this.val = value;
  }
}

我的境界对象是如下

My realm Object is as below

    public class RealmPerson extends RealmObject {
    @PrimaryKey
    private String userId;
    ...
    private RealmList<RealmString> stringStuff;
    private RealmList<SimpleRealmObj> otherStuff;

    <setters and getters>
   }

SimpleRealmObj工作正常,因为它只有一个字符串元素

SimpleRealmObj works fine as it only has String elements

    public class SimpleRealmObj extends RealmObject {
    private String foo;
    private String bar;
       ...
    }

我如何反序列化stringStuff?我试图用一个GSON TypeAdapter

How can I deserialize stringStuff? I tried using a gson TypeAdapter

public class RealmPersonAdapter extends TypeAdapter<RealmPerson> {
    @Override
    public void write(JsonWriter out, RealmPerson value) throws IOException {
        out.beginObject();
        Log.e("DBG " + value.getLastName(), "");
        out.endObject();
    }

    @Override
    public RealmPerson read(JsonReader in) throws IOException {
        QLRealmPerson rList = new RealmPerson();
        in.beginObject();
        while (in.hasNext()) {
            Log.e("DBG " + in.nextString(), "");
        }
        in.endObject();

        return rList;
    }

不过,我还是打了IllegalStateException异常

However I still hit the IllegalStateException

2334年至2334年/ com.qualcomm.qlearn.appË// PersonService.java:71:主要com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期字符串但名称第1行3列路径$

2334-2334/com.qualcomm.qlearn.app E//PersonService.java:71﹕ main com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was NAME at line 1 column 3 path $.

我试过RealmList,早期无果RealmString适配器。 唯一的解决办法我设法找到至今 https://github.com /域/领域-的Java /问题/ 620#issuecomment-66640786 任何更好的选择?

I tried RealmList, RealmString adapter earlier to no avail. The only workaround I managed to find so far is https://github.com/realm/realm-java/issues/620#issuecomment-66640786 Any better options?

推荐答案

该错误消息预计为字符串,但在名称可以通过检索的名称来解决在 JsonReader JSON对象的实际JSON对象之前(这是一个字符串你的情况)。

The error message "Expected a string but was NAME" can be solved by retrieving the name of the json object in the JsonReader before the actual json object (which is a String in your case).

您可以看看在的Andr​​oid文档JsonReader 。它有详细的说明和code段。您还可以看看在 readMessage 在文档中的示例code片段的方法。

You can take a look at the Android documentation for JsonReader. It has detailed explanation and code snippet. You can also take a look at the readMessage method in the sample code snippet in the documentation.

我修改了的方法是什么,我认为是应该的。 注意:我没有测试code,所以有可能会出现一些小错误在里面

I have modified your read method to what I think it should be. NOTE: I didn't test the code, so there may be some minor errors in it.

@Override
public RealmPerson read(JsonReader in) throws IOException {
    RealmPerson rList = new RealmPerson();
    in.beginObject();
    String name = "";
    while (in.hasNext()) {
        name = in.nextName();

        if (name.equals("userId")) {
            String userId = in.nextString();
            // update rList here 
        } else if (name.equals("otherStuff")) {
            // since otherStuff is a RealmList of RealmStrings,
            // your json data would be an array
            // You would need to loop through the array to retrieve 
            // the json objects
            in.beginArray();
            while (in.hasNext()) {
                // begin each object in the array
                in.beginObject();
                name = in.nextName();
                // the RealmString object has just one property called "value"
                // (according to the code snippet in your question)
                if (name.equals("val")) {
                    String val = in.nextString();
                     // update rList here 
                } else {
                    in.skipValue();
                }
                in.endObject();
            }
            in.endArray();
        } else {
            in.skipValue();
        }
    }
    in.endObject();


    return rList;
}

让我知道,如果这有助于。

Let me know if this helps.

阅读全文

相关推荐

最新文章