c# - Serializing to JSON exception "Type requires JSON array" -


i wanted try hand @ serializing data json.net saving game progress purpose , ease of testing. keep getting exception though, says:

additional information: cannot deserialize current json object (e.g. {"name":"value"}) type 'system.collections.generic.list`1[jsondatastorage.sword]' because type requires json array (e.g. [1,2,3]) deserialize correctly.

and i'm clueless @ causing exception.

take @ code:

using system; using system.collections.generic; using newtonsoft.json; using system.io;  class sword : iequatable<sword> {     public string name;     public int minimumdamage;     public int maximumdamage;       public sword(string name, int minimumdamage = 0, int maximumdamage = 0)     {         this.name = name;         this.minimumdamage = minimumdamage;         this.maximumdamage = maximumdamage;     }      public bool equals(sword other)     {         if (other == null) return false;         return (this.name.equals(other.name));      } }  class program {     static void main(string[]args)     {         // read file string , deserialize json type         console.writeline("reading data.json...");         string jsonstring = file.readalltext("data.json");         list<sword> mylist = jsonconvert.deserializeobject<list<sword>>(jsonstring);          if (mylist == null)             mylist = new list<sword>();          string input = "";         int inputminimumdamage = 0;         int inputmaximumdamage = 0;         string inputname = "";          while (input != "q")         {             console.writeline("press 'a' add new item");             console.writeline("press 'd' delete item");             console.writeline("press 's' show content");             console.writeline("press 'q' quit program");             console.write("press command: ");             input = console.readline();             switch (input) // switch on input string             {                 case "a":                     console.writeline("adding new item...");                     console.write("enter item name: ");                     inputname = console.readline();                     console.writeline("enter item minimum damage value: ");                     inputminimumdamage = convert.toint32(console.readline());                     console.writeline("enter item minimum damage value: ");                     inputmaximumdamage = convert.toint32(console.readline());                     mylist.add(new sword(inputname, inputminimumdamage, inputmaximumdamage));                     console.writeline("sword: " + inputname + "\ndamage stats: " + inputminimumdamage + "/" + inputmaximumdamage);                     break;                 case "d":                     console.writeline("deleting item");                     console.writeline("enter item name delete");                     inputname = console.readline();                     mylist.remove(new sword(inputname));                     console.writeline("deleted item name" + inputname);                     break;                 case "q":                     console.writeline("quitting program");                     break;                 case "s":                     console.writeline("showing contents");                     foreach (sword item in mylist)                     {                         console.writeline("sword: " + item.name + "\nstats: " + inputminimumdamage + "/" + inputmaximumdamage);                     }                     console.writeline();                     break;                 default:                     console.writeline("incorrect command, try again");                     break;             }         }          console.writeline("rewriting data.json");         string data = jsonconvert.serializeobject(mylist);         file.writealltext("data.json", data);         console.readline();     } } 

i'm getting error when deserialize list json format. so, that's that, thanks!


Comments