java - Realm can't create Nested Objects from Json -
i've been trying create nested realmobject using json creates first object , not nested ones. appreciate on this.
my realm classes:
content.java
public class content extends realmobject { private string uuid; realmlist<contentdetailmodel> contentdetail; public content() { super(); this.uuid = uuid.randomuuid().tostring(); } public string getuuid() { return uuid; } public realmlist<contentdetailmodel> getcontentdetails() { return contentdetail; } public void setcontentdetails(realmlist<contentdetailmodel> contentdetails) { this.contentdetail = contentdetails; } }
contentdetailmodel.java:
public class contentdetailmodel extends realmobject { string filename; string contenttypeid; realmlist<contentdetailmetadatamodel> contentdetailmetadata; realmresults<content> content = null; public string getfilename() { return filename; } public void setfilename(string filename) { filename = filename; } public string getcontenttypeid() { return contenttypeid; } public void setcontenttypeid(string contenttypeid) { contenttypeid = contenttypeid; } public realmlist<contentdetailmetadatamodel> getcontentdetailmetadata() { return contentdetailmetadata; } public void setcontentdetailmetadata(realmlist<contentdetailmetadatamodel> contentdetailmetadataz) { this.contentdetailmetadata = contentdetailmetadataz; } }
and rest of nested classes these. json string follows:
"
{ "content":{ "contentdetail":[ { "filename":"test.mp3", "contenttypeid":3, "contentdetailmetadata":{ "metadata":[ { "id":2, "value":"2017-08-02 09:40:30" }, { "id":1, "value":"35.73876557934912,51.50785446166992" } ] } }, { "filename":"2.jpg", "contenttypeid":2, "contentdetailmetadata":[ { "metadata":{ "id":2, "value":"2017-08-02 09:40:30" } }, { "metadata":{ "id":1, "value":"35.73876557934912,51.50785446166992" } } ] } ] } }
"
and code use :
realm.createobjectfromjson(json)
{ "content":{ "contentdetail":[ { "filename":"test.mp3", "contenttypeid":3, "contentdetailmetadata":[{ "metadata":[ { "id":2, "value":"2017-08-02 09:40:30" }, { "id":1, "value":"35.73876557934912,51.50785446166992" } ] }] },
translates to:
public class root extends realmobject { private content content; } public class content extends realmobject { private realmlist<contentdetail> contentdetail; @linkingobjects("content") private final realmresults<root> roots = null; } public class contentdetail extends realmobject { private string filename; private long contenttypeid; //private contentdetailmetadata contentdetailmetadata; private realmlist<contentdetailmetadata> contentdetailmetadata; @linkingobjects("contentdetail") private final realmresults<content> contents = null; } public class contentdetailmetadata extends realmobject { private realmlist<metadata> metadata; @linkingobjects("contentdetailmetadata") private final realmresults<contentdetail> contentdetails = null; } public class metadata extends realmobject { private long id; private string value; @linkingobjects("metadata") private final realmresults<contentdetailmetadata> contentdetailmetadatas = null; }
if schema doesn't that, createorupdatefromjson()
won't work.
personally advise against using schema though, it's pretty bad realm schema. it's advisable parse json , map schema makes more sense!
Comments
Post a Comment