0

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.

Mayur Lohite
  • 90
  • 1
  • 11
  • 3
    try changing `List services { get; set; }` to `public List services { get; set; }` – Mohammad Aghazadeh Oct 18 '22 at 07:29
  • Personally, I'd use `public IEnumerable Services { get; init; }`, as you (probably) don't want to modify the collection after it's been initialised. Also `class Services` is a bad name for a single item, the class name should be singular and the collection name should be plural. – Neil Oct 18 '22 at 07:51
  • @Neil I would argue that `IReadOnlyCollection` will do better in this case. – Guru Stron Oct 18 '22 at 17:38
  • @GuruStron https://stackoverflow.com/questions/491375/readonlycollection-or-ienumerable-for-exposing-member-collections/491591#491591 Unless the consumers of your methods are idiots and try casting back to the concrete type. – Neil Oct 19 '22 at 11:27
  • @Neil `IEnumerable` will not save in this case) – Guru Stron Oct 19 '22 at 11:32

1 Answers1

0

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; }
}
Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22
  • 2
    `List` would've been fine for the type of the `services` property. You didn't need to change the type. The key difference is that the `public` keyword was missing in the original code. Private properties aren't processed. – madreflection Oct 18 '22 at 07:26
  • I have done a very silly mistake. Thank you @Anand for pointing it out. – Mayur Lohite Oct 18 '22 at 07:52