0

i get some problem when i want to get the details count that i want

          {
        "hours": "09:30",
        "studios": [
            {
                "title": "event 11",
                "details": [
                    {
                        "studio_id": "1",
                        "studio_name": "event 11",
                        "show_name": "INLINE",
                        "is_first": false,
                        "studio_category": "Studio 9 & 11",
                        "intervals": 3
                    }
                ]
            },
            {
                "title": "event 12",
                "details": [
                    {
                        "studio_id": "2",
                        "studio_name": "event 12",
                        "show_name": "INLINE",
                        "is_first": false,
                        "studio_category": "Studio 9 & 11",
                        "intervals": 3
                    },
                    {
                        "studio_id": "2",
                        "studio_name": "event 12",
                        "show_name": "Dawn",
                        "is_first": true,
                        "studio_category": "Studio 9 & 11",
                        "intervals": 2

on top above are in List<Dictionary<string, dynamic>> how can i get a details count that way? i would like to use for loop but i can't get anything inside details

1 Answers1

0

One way is to map your classes.

public class X {
    public List<Studio> Studios { get; set;}
}

public class Studio {
    public List<Detail> Details { get; set;}
}

public class Detail {
    public string show_name {get; set; }
}

And then:

var json = File.ReadAllText("example2.json");

var x = JsonSerializer.Deserialize<X>(json, new JsonSerializerOptions{ PropertyNameCaseInsensitive = true});

var countOfStudios = x.Studios.Count; // 2

var countOfDetailsFromAllStudios = x.Studios.SelectMany(x => x.Details).Count(); // 3
tymtam
  • 31,798
  • 8
  • 86
  • 126