错误消息“
Expected a string but wasNAME”可以通过检索
JsonReader实际json对象之前的json对象的名称(
String在您的情况下为)来解决。
您可以看一下JsonReader的Android文档。它具有详细的说明和代码段。您也可以
readMessage在文档的示例代码片段中查看该方法。
我已经将您的
read方法修改为我认为的样子。 注意: 我没有测试代码,因此其中可能存在一些小错误。
@Overridepublic 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 pre 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;}让我知道是否有帮助。



