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

Sreejith Sree 1,251 Reputation points
2021-03-05T11:15:29.387+00:00

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?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sreejith Sree 1,251 Reputation points
    2021-03-15T08:17:17.79+00:00

    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 @JarvanZhang

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. JarvanZhang 23,936 Reputation points
    2021-03-05T13:41:14.94+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I don't know how to create a model class

    You could convert the json to model classes online (such as json2csharp). Then add the model classes in your project.

    how to show it on an expander

    Try using JsonConvert.DeserializeObject method to deserialize the JSON to a .NET object and set binding for the views in Xamarin.Forms.

       var tr = JsonConvert.DeserializeObject<List<Items>>(jsonString);  
       //After deserializing , we store our data in the List called ObservableCollection  
       ObservableCollection<Items> trends = new ObservableCollection<Items>(tr);  
    

    Similar issue you could refer to:
    https://stackoverflow.com/questions/60878223/xamarin-forms-read-local-json-file-and-display-in-picker

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.