c# - unable to update existing json list using jsonconverter -


i trying add field in json while deserializing response server , storing database

here model of response

 public class response : realmobject     {          [jsonconverter(typeof(quotationconverter))]         [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public ilist<quotation> quotationslist { get; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public ilist<order> orderslist { get; }     } 

quotationconverter code fetching customer name json , store quotation

 protected override ilist<quotation> parsearray(type objecttype, jarray jsonarray)         {              try             {                 realm realm = realm.getinstance();                 foreach (jobject data in jsonarray)                 {                      string customerid = data.getvalue("customerid").tostring();                     customer customer = realm.all<customer>().where(c => c.customerid == java.lang.long.parselong(customerid)).firstordefault();                     if (customer != null)                     {                         string customername = customer.customername;                         data.add("customername", customername);                     }                  }                  realm.dispose();                 var quotationslist = jsonarray.toobject<ilist<quotation>>();                  list<quotation> quotation = new list<quotation>(quotationslist);                  return quotationslist;             }             catch(exception e)             {                  debug.writeline(" exception "+e.stacktrace);             }              return null;         }          protected override quotation parseobject(type objecttype, jobject jsonobject)         {             throw new notimplementedexception();         }     } 

here jsonconverter

 public abstract class jsonconverter<t> : newtonsoft.json.jsonconverter     {         public override bool canconvert(type objecttype)         {             return (objecttype == typeof(jsonconverter<t>));          }          protected abstract t parseobject(type objecttype, jobject jsonobject);          protected abstract ilist<t> parsearray(type objecttype, jarray jsonarray);         public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)         {              try             {                              var jsonarray = jarray.load(reader);                var data=  parsearray(objecttype, jsonarray);                              return data;              }             catch(exception e)             {                 debug.writeline(e.stacktrace);             }             return null;          }          public override void writejson(jsonwriter writer, object value, jsonserializer serializer)         {              debug.writeline("mo " + value);           }     }  issue when getting response object inside quotationslist coming blank. 

json received server

quotationslist: [   {     "account": null,     "contactid": 0,     "currency": "usd",     "customerid": 5637144583,     "deliveryaddress": "19th , edwardsville rd (rt203)\ngranite city,il62040\nusa",     "expirydate": "2017-09-04",     "followupdate": "2017-09-01",     "mquotationid": null,     "opportunityid": 0,     "postedon": null,     "prospectid": 0,     "quotationfor": null,     "quotationid": 5637155076,     "quotationname": "united states steel",     "quotationnumber": "ust1-000022",     "quotationstatus": "confirmed",     "requestreceiptdate": "2017-08-05",     "requestshipdate": "2017-08-05",     "siteid": "glen1",     "warehouseid": "37"   } 

expected json

quotationslist: [   {     "account": null,     "contactid": 0,     "currency": "usd",     "customerid": 5637144583,     "deliveryaddress": "19th , edwardsville rd (rt203)\ngranite city,il62040\nusa",     "expirydate": "2017-09-04",     "followupdate": "2017-09-01",     "mquotationid": null,     "opportunityid": 0,     "postedon": null,     "prospectid": 0,     "quotationfor": null,     "quotationid": 5637155076,     "quotationname": "united states steel",     "quotationnumber": "ust1-000022",     "quotationstatus": "confirmed",     "requestreceiptdate": "2017-08-05",     "requestshipdate": "2017-08-05",     "siteid": "glen1",     "warehouseid": "37",     "customername":"jhon caro"   } 

quotation model

 public class quotation : realmobject , imaster, imedia, iproduct     {         [primarykey]         [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public string mquotationid { get; set; } = guid.newguid().tostring();          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public long quotationid { get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public string customername { get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public string quotationname { get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public string quotationnumber{ get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public string deliveryaddress { get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public string expirydate { get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public string requestshipdate { get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public string requestreceiptdate { get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public long prospectid { get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public string followupdate { get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public long opportunityid { get; set; }          [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]         public string postedon { get; set; }        } 

updated

protected override ilist<quotation> parsearray(type objecttype, jarray jsonarray) {      try     {         realm realm = realm.getinstance();         var data = jsonarray.toobject<ilist<quotation>>();         list<quotation> quotationlist = new list<quotation>(data);            foreach (quotation quotation in quotationlist)         {              long customerid = quotation.customerid;             customer customer = realm.all<customer>().where(c => c.customerid == customerid).firstordefault();             if (customer != null)             {                 string customername = customer.customername;                 quotation.customername = customername;             }          }          realm.dispose();           return quotationlist;     }     catch(exception e)     {          debug.writeline(" exception "+e.stacktrace);     }      return null; } 

enter image description here

this how deserialize called

response responsedata = await task.run(() => jsonconvert.deserializeobject(content));

the issue here not json deserialize or add json array, issue have no record coming database.

you using this:

 long customerid = quotation.customerid;             customer customer = realm.all<customer>().where(c => c.customerid == customerid).firstordefault();             if (customer != null)             {                 string customername = customer.customername;                 quotation.customername = customername;             } 

firstordefault expect 0 or more items returned query want access first item in code (i.e. not sure if item given key exists)

change first(); , see if throws , exception, if issue , your:

realm.all<customer>().where(c => c.customerid == customerid) 

is returning incorrect data.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -