android - Deserializing and Serializing JSON File in C# -


i'm trying serialize object(item) file gets appended more item objects each time function called.

it keeps giving me error:

07-12 15:43:27.931 i/monodroid(17468): newtonsoft.json.jsonserializationexception: cannot deserialize current json object (e.g. {"name":"value"}) type 'system.collections.generic.list`1[waterapp.item]' because type requires json array (e.g. [1,2,3]) deserialize correctly. 07-12 15:43:27.931 i/monodroid(17468): fix error either change json json array (e.g. [1,2,3]) or change deserialized type normal .net type (e.g. not primitive type integer, not collection type array or list) can deserialized json object. jsonobjectattribute can added type force deserialize json object.

here object class:

using system; using system.collections; using android.content; using android.preferences; using java.io;  namespace waterapp {     public class item     {         public bool type { get; set; }         public string itemname { get; set; }         public datetime expirydate { get; set; }         public int itemiconnumber { get; set; }          public item(bool type, string itemname, datetime expirydate, int itemiconnumber)         {             type = type;             itemname = itemname;             expirydate = expirydate;             itemiconnumber = itemiconnumber;         }     } } 

here class data handling done methods:

using system; using system.collections; using system.collections.generic; using system.io; using newtonsoft.json; using console = system.console; using environment = system.environment; using file = system.io.file; using jsonwriter = newtonsoft.json.jsonwriter;  namespace waterapp.droid {     public abstract class datahandler     {          private static string documentspath = system.environment.getfolderpath(environment.specialfolder.personal);         private static string filepath = path.combine(documentspath, "hatchlistdata.json");          public static void saveobjecttofile(item item)     {         // read in existing list         // important - if first time , file doesn't exist,         // need initialise itemlist = new list<item>();         var itemlist = new list<item>();         file.writealltext(filepath, string.empty);         string json = file.readalltext(filepath);         itemlist = jsonconvert.deserializeobject<list<item>>(json);          // add new item         itemlist.add(item);          // serialize list , write out again         json = jsonconvert.serializeobject(itemlist, formatting.indented);         file.writealltext(filepath, json);         console.writeline(file.readalltext(filepath));     }          public static void loadlist(list<item> listtobeloaded)         {             string json = "";              if (file.exists(filepath))             {                 if (filepath.length > 2)             {                 console.writeline("reading" + "read!");                  using (streamreader streamreader = new streamreader(filepath))                 {                     string json = streamreader.readtoend();                     listtobeloaded = jsonconvert.deserializeobject<list<item>>(json);                 }                  console.writeline("reading" + "loaded");             }             }             else             {                 console.writeline("reading" + "doesn't exist, json file.");             }         }     } } 

please can push me in right direction or fix error in way? i'm not sure how fix issue of error. don't understand code error asking for.

you can't append 2 serialized json objects - won't produce valid json. instead, need read in existing list, add items it, , serialize out again

// read in existing list // important - if first time , file doesn't exist, // need initialise itemlist = new list<item>(); json = file.readalltext(filepath); var itemlist = jsonconvert.deserializeobject<list<item>>(json);  // add new item itemlist.add(newitem);  // serialize list , write out again string json = jsonconvert.serializeobject(itemlist, formatting.indented); file.writealltext(path,json); 

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 -