c# Deserialize JSON list of object error -


when deserialize list of object work when deserialize object list type errors out. idea how make work?

page name: testjson.aspx

using system; using system.collections.generic; using system.web.script.serialization;  namespace web.json {     public partial class testjson : system.web.ui.page     {         protected void page_load(object sender, eventargs e)         {             string json = "[{\"sequencenumber\":1,\"firstname\":\"fn1\",\"lastname\":\"ln1\"},{\"sequencenumber\":2,\"firstname\":\"fn2\",\"lastname\":\"ln2\"}]";               //this work             ilist<person> persons = new javascriptserializer().deserialize<ilist<person>>(json);              //this error             //people persons = new javascriptserializer().deserialize<people>(json);               response.write(persons.count());         }     }      class person     {         public int sequencenumber { get; set; }         public string firstname { get; set; }         public string lastname { get; set; }     }      class people : list<person>     {         public people()         {          }         public people(ienumerable<person> init)         {             addrange(init);                     }     } 

error message: value "system.collections.generic.dictionary`2[system.string,system.object]" not of type "json.person" , cannot used in generic collection.

i suggest doing this:

    people persons = new people(new javascriptserializer().deserialize<ilist<person>>(json)); 

and changing constructor this:

    public people(ienumerable<person> collection) : base(collection)     {      } 

you don't have worry messy casts between types, , works since people class has base constructor takes in ienumberable.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -