question

SreejithSree-2948 avatar image
0 Votes"
SreejithSree-2948 asked ·

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?

dotnet-xamarinforms
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SreejithSree-2948 avatar image
0 Votes"
SreejithSree-2948 answered ·

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-MSFT

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered ·

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.



· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried the json2csharp and getting model class like below:

 public class 20210503T0500000000000
 {
     public string title { get; set; }
     public string description { get; set; }
 }
 public class 20210504T0500000000000
 {
     public string title { get; set; }
     public string description { get; set; }
 }
 public class 20210505T0500000000000
 {
     public string title { get; set; }
     public string description { get; set; }
 }
 public class CalendarEvents
 {
     [JsonProperty("2021-05-03T05:00:00.000+0000")]
     public List<_20210503T0500000000000> _20210503T0500000000000 { get; set; }

     [JsonProperty("2021-05-04T05:00:00.000+0000")]
     public List<_20210504T0500000000000> _20210504T0500000000000 { get; set; }

     [JsonProperty("2021-05-05T05:00:00.000+0000")]
     public List<_20210505T0500000000000> _20210505T0500000000000 { get; set; }
 }


Because of the character limitation posting the remaining code below:

0 Votes 0 ·
 public class Root
 {
     public CalendarEvents calendarEvents { get; set; }
 }

It creates a lot of classes using the DateTime value(20210505T0500000000000). So creating a model class above is impossible. How can I create a generic model class for this and how can I Bind the data like DateTime(20210505T0500000000000) and title(Event 3) to the expander?

0 Votes 0 ·

Is '20210505T0500000000000' a DateTime value? But according to the json string, it has two parameters. The json data shown below should be converted to item class. Please mark sure the json data is correct.

TodoItem: 
[
  {
    "property_1": "value_1",
    "property_2": "value_2"
  }
]

public class TodoItem
{
    public string property_1 { get; set; }
    public string property_2 { get; set; }
}
0 Votes 0 ·