c# Deserialize JSON list of object error

后端 未结 1 1636
日久生厌
日久生厌 2021-01-24 23:30

when I deserialize to list of object it work but when I deserialize to a object with list type it errors out. Any idea how to make it work?

page name: testjson.aspx

相关标签:
1条回答
  • 2021-01-25 00:06

    I would suggest doing something like this:

        People persons = new People(new JavaScriptSerializer().Deserialize<IList<Person>>(json));
    

    and changing your constructor to this:

        public People(IEnumerable<Person> collection) : base(collection)
        {
    
        }
    

    You don't have to worry about messy casts between types, and it works just as well since your People class has a base constructor that takes in an IEnumberable.

    0 讨论(0)
提交回复
热议问题