Gson将List 反序列化为realmList
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
我正在使用gson进行改造,将我的json反序列化为领域对象。这在大多数情况下非常有效。处理时遇到麻烦
RealmList(String(or any other basic data type))
RealmList(String(或任何其他基本数据类型))
Since Realm doesnt support RealmList where E doesnt extend Realm object, I wrapped String in a RealmObject.
由于Realm不支持RealmList,其中E不扩展Realm对象,我将String包装在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 works fine as it only has String elements
SimpleRealmObj工作正常,因为它只有String元素
public class SimpleRealmObj extends RealmObject {
private String foo;
private String bar;
...
}
How can I deserialize stringStuff? I tried using a gson TypeAdapter
如何反序列化stringStuff?我尝试使用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;
}
However I still hit the IllegalStateException
但是我仍然遇到了IllegalStateException
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 $.
2334-2334 / com.qualcomm.qlearn.app E // PersonService.java:71:main com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:期望一个字符串但在第1行第3列路径$为NAME。
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?
我之前试过RealmList,RealmString适配器无济于事。到目前为止,我设法找到的唯一解决方法是https://github.com/realm/realm-java/issues/620#issuecomment-66640786任何更好的选择?
4 个解决方案
#1
5
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).
错误消息“Expected a string but is NAME”可以通过在实际json对象(在您的情况下为String)之前检索JsonReader中的json对象的名称来解决。
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.
您可以查看JsonReader的Android文档。它有详细的解释和代码片段。您还可以查看文档中示例代码段中的readMessage方法。
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.
如果这有帮助,请告诉我。
更多相关文章
- 在Java中,使用DefaultSelenium对象在selenium中启动测试,我如何找
- java SSH员工管理系统以及Demo代码下载
- 如何在命令中传递对象参数?
- JavaScript 面向对象编程,严格过程的标准化编程法,目前为止最好的
- JAVA复习3 Java类和对象
- 8/人天,小记一次 JAVA(APP后台) 项目改造 .NET 过程(后台代码已完整
- java对象判断是否为空工具类
- Java借助Runtime调用外部程序阻塞的代码
- Spring SpEL系列二 @Value,xml, Java代码使用SpEL