Home > Enterprise >  JSON is not deserlizing using Newtonsoft
JSON is not deserlizing using Newtonsoft

Time:10-18

Hello here is my response.

{
  "services": [
    {
      "ServiceId": 222977,
      "ServiceName": "Mayur  Lohite"
    }
  ]
}

Here is my class

public class ServiceContainer
{
    List<Services> services { get; set; }
}
public class Services
{
    public int ServiceId { get; set; }
    public string? ServiceName { get; set; }
}

I am trying to convert like this

ServiceContainer? services = JsonConvert.DeserializeObject<ServiceContainer>(response.ToString());

But not getting the data in services list.

CodePudding user response:

For the JSON that you are showing, the below would be the appropriate class,

public class ServiceContainer
{
    public Service[] services { get; set; }
}

public class Service
{
    public int ServiceId { get; set; }
    public string ServiceName { get; set; }
}

CodePudding user response:

try something like this

ServiceContainer? sc = JsonConvert.DeserializeObject<List<Services>>(response.ToString());
  • Related