Search code examples
c#restsharp

Error: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1


I'm trying to deserialize an API json response into a list. BUt I get teh error: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1

Here is the code which I'm using to deserialize:

 var response = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EndofDay.Insrtumentxxx>>(Content);

Here is my class that contains the list:

public class EndofDay
{

    public Instrumentxxx instrumentxxx { get; set; }

    public class Instrumentxxx
    {
        public string id { get; set; }
        public double APrice { get; set; }
        public double BPrice { get; set; }
        public double CPrice { get; set; }


    }
}

}

Here is my json api response:

{\"instrumentxxx\":
[{\"id\":\"B1QH8P2\",
\"APrice\":5.83,
\"BPrice\":6.83,
\"CPrice\":7.83,}]}

What am I missing?


Solution

  • Your JSON response is array and you have declared as single object. You must declare instrumentxxx as List<Instrumentxxx>

    Here is the working code Click here

    public class EndofDay
    {
    
        public List<Instrumentxxx> instrumentxxx { get; set; }   
        
    }
    
    public class Instrumentxxx
    {
       public string id { get; set; }
       public double APrice { get; set; }
       public double BPrice { get; set; }
       public double CPrice { get; set; }   
    }