I use the Realm database, there is the following json object that I need to dump from the file and save to the database.

{ "pPID": 1, "pName": "myName", "pDesc": "myDescription", "pTotal": 120, "pOrder": 1, "puoCompleted": false, "puoProgressPercent":0, "puoProgressCount":0, "pCommercialAccessRule": { "tag": "myTag", "contents": [ "string object", "string object" ] } } 

A problem appears on the pCommercialAccessRule object, which has a list of strings. primitive types are not supported, and for this I created an object

 public class RealmString extends RealmObject{ String string; public String getString() { return string; } public void setString(String string) { this.string = string; } } 

And in the CommercialAccessRule class, I created a list of RealmString objects, not strings.

 public class CommercialAccessRule extends RealmObject implements Serializable { private String tag; private RealmList<RealmString> contents; public CommercialAccessRule() { } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } public RealmList<RealmString> getContents() { return contents; } public void setContents(RealmList<RealmString> contents) { this.contents = contents; } } 

But I get the following error

 Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at android.util.JsonReader.expect(JsonReader.java:310) at android.util.JsonReader.beginObject(JsonReader.java:293) at io.realm.RealmStringRealmProxy.createUsingJsonStream(RealmStringRealmProxy.java:136) at io.realm.CommercialAccessRuleRealmProxy.createUsingJsonStream(CommercialAccessRuleRealmProxy.java:390) at io.re Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRINGalm.PDataRealmProxy.createUsingJsonStream(PDataRealmProxy.java:525) at io.realm.DefaultRealmModuleMediator.createUsingJsonStream(DefaultRealmModuleMediator.java:257) at io.realm.Realm.createAllFromJson(Realm.java:435) 

Googling got here , and then here. But I can't figure out how to put this logic in here:

  InputStream stream = null; try { stream = getApplicationContext().getAssets().open("test_pdata.json"); } catch (IOException e) { e.printStackTrace(); } try { realm.beginTransaction(); realm.createAllFromJson(PData.class, stream); realm.commitTransaction(); } catch (IOException e) { realm.cancelTransaction(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } 

Here is the class PData json which I cited above

 public class PData extends RealmObject implements Serializable{ private static final String TAG = PData.class.getSimpleName(); private String pName; private String pDesc; private boolean puoCompleted; private int pPID; private int pTotal; private int pOrder; private int puoProgressCount; private int puoProgressPercent; private CommercialAccessRule pCommercialAccessRule; private String originalJson; public PData() { } public String getpName() { return pName; } public void setpName(String pName) { this.pName = pName; } public String getpDesc() { return pDesc; } public void setpDesc(String pDesc) { this.pDesc = pDesc; } public boolean ispuoCompleted() { return puoCompleted; } public void setpuoCompleted(boolean puoCompleted) { this.puoCompleted = puoCompleted; } public int getpPID() { return pPID; } public void setpPID(int pPID) { this.pPID = pPID; } public int getpTotal() { return pTotal; } public void setpTotal(int pTotal) { this.pTotal = pTotal; } public int getpOrder() { return pOrder; } public void setpOrder(int pOrder) { this.pOrder = pOrder; } public int getpuoProgressCount() { return puoProgressCount; } public void setpuoProgressCount(int puoProgressCount) { this.puoProgressCount = puoProgressCount; } public int getPuoProgressPercent() { return puoProgressPercent; } public void setPuoProgressPercent(int puoProgressPercent) { this.puoProgressPercent = puoProgressPercent; } public CommercialAccessRule getCommercialAccessRule() { return pCommercialAccessRule; } public void setpCommercialAccessRule(CommercialAccessRule pCommercialAccessRule) { this.pCommercialAccessRule = pCommercialAccessRule; } public String getOriginalJson() { return originalJson; } public void setOriginalJson(String originalJson) { this.originalJson = originalJson; } @Override public String toString() { return "PData{" + "pName='" + pName + '\'' + ", pDesc='" + pDesc + '\'' + ", puoCompleted=" + puoCompleted + ", pPID=" + pPID + ", pTotal=" + pTotal + ", pOrder=" + pOrder + ", puoProgressCount=" + puoProgressCount + ", puoProgressPercent=" + puoProgressPercent + ", pCommercialAccessRule=" + pCommercialAccessRule + ", originalJson='" + originalJson + '\'' + '}'; } } 

    0