Json nested objects and how to use with ObservableCollection<>

Robert S 41 Reputation points
2021-02-17T04:36:24.297+00:00

I'm working in Xamarin and have a nested json object (which is a list of items starting at "message") that I need to add to an ObservableCollection, but I'm having trouble assigning it to the collection. The data is getting retrieved fine, but I keep getting this error:

cannot convert from 'RequestorPerformer.Models.BaseVenue[]' to 'System.Collections.Generic.IEnumerable<RequestorPerformer.Models.Rootobject>'

The model:

   public class BaseVenue 
    {
        public int Id { get; set; }            
        public string VenueName { get; set; }
        public string VenueAddress { get; set; }
        public string ShowDate { get; set; }  
        public string ShowTime { get; set; }        
        public string Description { get; set; }
    }


    public class Rootobject
    {
        public bool Error { get; set; }
        public int ErrorCode { get; set; }
        public BaseVenue[] Message { get; set; }
    }

The code (the line, "_posts = new ..." is where I'm getting the error) :

private ObservableCollection<Rootobject> _posts;
var posts = JsonConvert.DeserializeObject<Rootobject>(returnResponse);
_posts = new ObservableCollection<Rootobject>(posts.Message);

The data (what's in returnResponse):

{
    "error": false,
    "errorCode": 0,
    "message": [{
        "id": "93",
        "venueName": "Sushi Kuni",
        "venueAddress": "10211 S De Anza Blvd Cupertino CA",
        "showDate": "1531022400",
        "showTime": "",
        "description": ""
    }, {
        "id": "38",
        "venueName": "Two Keys Tavern",
        "venueAddress": "333 S Limestone Lexington KY",
        "showDate": "1531368000",
        "showTime": "8 pm - 1 am",
        "description": ""
    }, {
        "id": "39",
        "venueName": "Two Keys Tavern",
        "venueAddress": "333 S Limestone Lexington KY",
        "showDate": "1531454400",
        "showTime": "8 pm - 2 am",
        "description": ""
    }]
}

Even if I remove the posts.Message and just make it post I get the same basic error message:

cannot convert from 'RequestorPerformer.Models.Rootobject' to 'System.Collections.Generic.IEnumerable<RequestorPerformer.Models.Rootobject>'

Thank you in advance,

Robert

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

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-02-17T05:41:24.19+00:00

    Hello,

    Welcome to Microsoft Q&A!

    We should make the variable type same as the generic T type passed when calling JsonConvert.DeserializeObject() .

    T = JsonConvert.DeserializeObject<T>() ;

    Change your code as below .

       private Rootobject posts;  
       var posts = JsonConvert.DeserializeObject<Rootobject>(returnResponse);  
       BaseVenue[] Message = posts.Message ;  
    

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful