c# - Realm JSON parsing issue list/array -


i'm running in issue realm , inability use basic lists in object scheme someobject : realmobject. i'm parsing json objects web directly in realm objects. it's not mapping should array parts, in json data particularly "entrycharts" data. here json web. take @ entrycharts array.

 {     "id": 20,     "tradetype": "buy",     "title": "enter: @ market (1,144p)",     "keypoints": "<ul><li><strong>enter:</strong> @ market (1,144p)</li><li><strong>stop:</strong> 1107p</li></ul>",     "productid": 2,     "showasfeatured": false,     "entrysummary": "<p>lorem ipsum dolor sit amet, consectetur adipiscing elit. semper in malesuada id, varius sit amet lectus.&nbsp;</p>\n",     "entrycharts": [       {         "data": "https://www.somesite.co.uk/somepic.png"       }     ],     "entrydate": "2016-06-22t11:32:53.22",     "exitsummary": "",     "takeprofitsdate": null,     "stophitdate": null,     "createdateutc": "2016-06-22t11:34:30.04",     "status": "live"   }, 

here's realmobject

public class report : realmobject {     [objectid]     public int id { get; set; }     public string tradetype { get; set; }     public string title { get; set; }     public string keypoints { get; set; }     public int productid { get; set; }     public bool showasfeatured { get; set; }     public string entrysummary { get; set; }     public realmlist<entrychart> entrycharts { get; }     public string entrydate { get; set; }     public string exitsummary { get; set; }     public string takeprofitsdate { get; set; }     public string stophitdate { get; set; }     public string createdateutc { get; set; }     public string status { get; set; }     public product product { get; set; }  }  public class entrychart : realmobject {     public string data { get; set; } } 

based on how supposed make arrays of basic types such string nested objects looks should work me it's not parsing correctly. guess because realmlist doesn't have setter can't make instance of realmlist object inside json parser.

the problem realm objects need created using realm.createobject<t>() method list relationships work (see https://github.com/realm/realm-dotnet/issues/514).

luckily, newtonsoft.json has ability populate existing object values json. way you'll able create report objects createobject , pass them json serializer. newtonsoft.json can deserialize intermediate type (the newtonsoft.json.linq.jtoken family of classes) can manipulated , further deserialized concrete type.

you can deserialize json payload jarray , every element in create report object , populate it:

var serializer = jsonserializer.createdefault(); var jsonarray = jarray.parse(jsonstring); var reports = new list<report>(); realm.write(() => {     foreach (var jsonvalue in jsonarray)     {         var report = realm.createobject<report>();         serializer.populate(new jtokenreader(jsonvalue), report);         reports.add(report);     } }); 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -