c# - JSON.NET - Object update with List -


could suggest method of updating items in cheese.producers list?

i have following classes:

class producer {     public string name { get; set; }     public int rating { get; set; }      public producer()     {     }      public producer(string name, int rating)     {         name = name;         rating = rating;     } }  class cheese {     public string name { get; set; }     public int age { get; set; }     public string taste { get; set; }     public list<producer> producers { get; set; }      public cheese()     {         producers = new list<producer>();     }      public cheese(string name, int age)     {         name = name;         age = age;         producers = new list<producer>();     }      public cheese(string name, int age, string taste)     {         name = name;         age = age;         taste = taste;         producers = new list<producer>();     } } 

in main code have object(gouda) want update based on json read file.

static void main(string[] args) {     producer prod1 = new producer("prod1", 5);     producer prod2 = new producer("prod2", 6);     producer prod3 = new producer("prod3", 7);      cheese gouda = new cheese("gouda", 5, "mild");     gouda.producers.add(prod1);     gouda.producers.add(prod2);     gouda.producers.add(prod3);      string propertiestobeadded = file.readalltext("properties.txt");     jsonserializersettings jsonserializersettings = new jsonserializersettings     {         objectcreationhandling = objectcreationhandling.reuse     };     jsonconvert.populateobject(propertiestobeadded, gouda, jsonserializersettings); } 

the json update file:

{   "name": "hard blue",   "taste": "sharp",   "producers": [     {       "name": "prod1",       "rating": 100     },     {       "name": "prod3",       "rating": 300     }   ] } 

the major problem when populateobject called, instead of updating producers list items, 2 new members added. other fields seem work fine. suggestions?

try this:

                producer prod1 = new producer("prod1", 5);                 producer prod2 = new producer("prod2", 6);                 producer prod3 = new producer("prod3", 7);                  cheese gouda = new cheese("gouda", 5, "mild");                 gouda.producers.add(prod1);                 gouda.producers.add(prod2);                 gouda.producers.add(prod3);                  var propertiestobeadded = file.readalltext(@"c:\json path");                  var settings = new jsonmergesettings                 {                     mergearrayhandling = mergearrayhandling.merge                 };                  var o1 = jobject.parse(jsonconvert.serializeobject(gouda));                  o1.merge(jobject.parse(propertiestobeadded), settings);                  var o = o1.tostring(); 

and need change json format bit :

{   'name': 'hard blue',   'taste': 'sharp',   'producers': [     {       'name': 'prod1',       'rating': 100     },     {      },     {       'name': 'prod3',       'rating': 300     }   ] } 

here go:

enter image description here

hope helps.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -