jsonxamarin.formsexpander

Xamarin Forms: How to parse a JSON response without a stable key?


I am using Newtonsoft.Json to parse my response. But for the below response I don't know how to create a model class and how to show it on an expander.

My Response:

{
"calendarEvents": {
        "2021-05-03T05:00:00.000+0000": [
            {
                "title": "Event 2",
                "description": "Event 2"
            }
        ],
        "2021-05-04T05:00:00.000+0000": [
            {
                "title": "Event 3",
                "description": "Event 3"
            }
        ],
        "2021-05-05T05:00:00.000+0000": [
            {
                "title": "Event 3",
                "description": "Event 3"
            },
            {
                "title": "Event 4",
                "description": "Event 4"
            }
        ]
    }
}

I tried something like below:

public class MyEvents
{
    public List<calendarEvents> calendarEvents { get; set; }
}

public class calendarEvents
{
    //What will be here? it is also a list and it has no stable key
}

public class Dummy//(Top class name here)
{
    public string title { get; set; }
    public string description { get; set; }
}

Instead of 2021-05-03T05:00:00.000+0000 this what I can add to the model class? The response is a list of items inside another list. Plan to use an expander to show this on UI, so any extra implementation need for that?


Solution

  • I checked with the back end team and they changed the JSON response like below:

    {
        "calendarEvents": [
            {
                "day": "20210503",
                "list": [
                    {
                        "title": "Event 3",
                        "description": "Event 3"
                    }
                ]
            },
            {
                "day": "20210504",
                "list": [
                    {
                        "title": "Event 3",
                        "description": "Event 3"
                    },
                    {
                        "title": "Event 4",
                        "description": "Event 4"
                    }
                ]
            },
            {
                "day": "20210505",
                "list": [
                    {
                        "title": "Event 3",
                        "description": "Event 3"
                    },
                    {
                        "title": "Event 4",
                        "description": "Event 4"
                    },
                    {
                        "title": "Event 5",
                        "description": "Event 5"
                    }
                ]
            }
        ]
    }
    

    Corressponding Model Class:

    public class List
    {
        public string title { get; set; }
        public string description { get; set; }
    }
    
    public class CalendarEvent
    {
        public string day { get; set; }
        public List<List> list { get; set; }
    }
    
    public class Root
    {
        public List<CalendarEvent> calendarEvents { get; set; }
    }
    

    Now I am able to parse the data and show it on an expander.

    Thanks