BindableLayout cannot convert to 'System.Collections.IEnumerable'

Jassim Al Rahma 1,516 Reputation points
2021-11-28T20:30:15.227+00:00

Hi,

I am trying to read the REST from:

https://documentation.onesignal.com/reference/view-notification

but getting:

cannot convert to 'System.Collections.IEnumerable'

I tried both:

Root data = JsonSerializer.Deserialize<Root>(result);

and:

Root data = (Root)JsonSerializer.Deserialize<IEnumerable<Root>>(result);

This is my code:

var client = new HttpClient();

UriBuilder builder = new UriBuilder(string.Format("https://onesignal.com/api/v1/notifications/{0}", message_id));
builder.Query = "app_id=" + SecureStorage.GetAsync("AppID").Result;

client.DefaultRequestHeaders.Add("authorization", "Basic " + SecureStorage.GetAsync("AppAuthID").Result);

var response = await client.GetAsync(builder.Uri);

var result = await response.Content.ReadAsStringAsync();

// Root data = JsonSerializer.Deserialize<Root>(result);
Root data = (Root)JsonSerializer.Deserialize<IEnumerable<Root>>(result);

BindableLayout.SetItemsSource(ScrollViewNotification, data);

Here is the Class:

public class Contents
{
    public string ar { get; set; }
    public string en { get; set; }
}

public class Data
{
}

public class Headings
{
}

public class ApnsAlert
{
}

public class Ios
{
    public int successful { get; set; }
    public int failed { get; set; }
    public int errored { get; set; }
    public int converted { get; set; }
    public int received { get; set; }
}

public class Android
{
    public int successful { get; set; }
    public int failed { get; set; }
    public int errored { get; set; }
    public int converted { get; set; }
    public int received { get; set; }
}

public class PlatformDeliveryStats
{
    public Ios ios { get; set; }
    public Android android { get; set; }
}

public class Root
{
    public object adm_big_picture { get; set; }
    public object adm_group { get; set; }
    public object adm_group_message { get; set; }
    public object adm_large_icon { get; set; }
    public object adm_small_icon { get; set; }
    public object adm_sound { get; set; }
    public object spoken_text { get; set; }
    public object alexa_ssml { get; set; }
    public object alexa_display_title { get; set; }
    public object amazon_background_data { get; set; }
    public object android_accent_color { get; set; }
    public object android_group { get; set; }
    public object android_group_message { get; set; }
    public object android_led_color { get; set; }
    public object android_sound { get; set; }
    public int android_visibility { get; set; }
    public string app_id { get; set; }
    public object big_picture { get; set; }
    public object buttons { get; set; }
    public bool canceled { get; set; }
    public object chrome_big_picture { get; set; }
    public object chrome_icon { get; set; }
    public object chrome_web_icon { get; set; }
    public object chrome_web_image { get; set; }
    public object chrome_web_badge { get; set; }
    public object content_available { get; set; }
    public Contents contents { get; set; }
    public int converted { get; set; }
    public Data data { get; set; }
    public object delayed_option { get; set; }
    public object delivery_time_of_day { get; set; }
    public int errored { get; set; }
    public List<object> excluded_segments { get; set; }
    public int failed { get; set; }
    public object firefox_icon { get; set; }
    public object global_image { get; set; }
    public Headings headings { get; set; }
    public string id { get; set; }
    public object include_player_ids { get; set; }
    public object include_external_user_ids { get; set; }
    public List<string> included_segments { get; set; }
    public object thread_id { get; set; }
    public int ios_badgeCount { get; set; }
    public object ios_badgeType { get; set; }
    public object ios_category { get; set; }
    public string ios_interruption_level { get; set; }
    public double ios_relevance_score { get; set; }
    public object ios_sound { get; set; }
    public ApnsAlert apns_alert { get; set; }
    public object target_content_identifier { get; set; }
    public bool isAdm { get; set; }
    public bool isAndroid { get; set; }
    public bool isChrome { get; set; }
    public bool isChromeWeb { get; set; }
    public object isAlexa { get; set; }
    public bool isFirefox { get; set; }
    public bool isIos { get; set; }
    public bool isSafari { get; set; }
    public bool isWP { get; set; }
    public bool isWP_WNS { get; set; }
    public bool isEdge { get; set; }
    public bool isHuawei { get; set; }
    public bool isSMS { get; set; }
    public object large_icon { get; set; }
    public int priority { get; set; }
    public int queued_at { get; set; }
    public int remaining { get; set; }
    public int send_after { get; set; }
    public int completed_at { get; set; }
    public object small_icon { get; set; }
    public int successful { get; set; }
    public object received { get; set; }
    public object tags { get; set; }
    public object filters { get; set; }
    public object template_id { get; set; }
    public int ttl { get; set; }
    public object url { get; set; }
    public object web_url { get; set; }
    public object app_url { get; set; }
    public object web_buttons { get; set; }
    public object web_push_topic { get; set; }
    public object wp_sound { get; set; }
    public object wp_wns_sound { get; set; }
    public PlatformDeliveryStats platform_delivery_stats { get; set; }
    public object ios_attachments { get; set; }
    public object huawei_sound { get; set; }
    public object huawei_led_color { get; set; }
    public object huawei_accent_color { get; set; }
    public string huawei_visibility { get; set; }
    public object huawei_group { get; set; }
    public object huawei_group_message { get; set; }
    public object huawei_channel_id { get; set; }
    public object huawei_existing_channel_id { get; set; }
    public object huawei_small_icon { get; set; }
    public object huawei_large_icon { get; set; }
    public object huawei_big_picture { get; set; }
    public object huawei_msg_type { get; set; }
    public object throttle_rate_per_minute { get; set; }
    public object sms_from { get; set; }
    public object sms_media_urls { get; set; }
    public object name { get; set; }
    public object email_click_tracking_disabled { get; set; }
    public object fcap_group_ids { get; set; }
    public string fcap_status { get; set; }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,288 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,179 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 25,586 Reputation points Microsoft Vendor
    2021-12-01T03:24:38.67+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    I'm afraid you need to change the Root data = (Root)JsonSerializer.Deserialize<IEnumerable<Root>>(result); to

     Root data = JsonConvert.DeserializeObject<Root>(result);  
    

    From the Txt file your provided, we can see this result is not a list, the json formart is {......}, so it cannot convert to System.Collections.IEnumerable , only this formart [......] can be converted .

    In addition, android_visibility , ios_badgeCount, ios_relevance_score , isSMS , priority , ttl these fields are null in the Txt file your provided, however, they cannot be null. So, I change these Property Type to object in class Root.

    public object android_visibility { get; set; }  
    

    For more information about IEnumerable Interface , you could refer to
    https://learn.microsoft.com/en-us/dotnet/api/system.collections.ienumerable?view=net-6.0

    ------------update-----------
    According to your StackLayout, if you use BindableLayout.SetItemsSource, the layout is a collection, you could set ItemTemplate, refer to the following code :

     var data = JsonConvert.DeserializeObject<Root>(json);  
     var bindObject = new Root[]  
                     {  
                        data,  
                     };  
     BindableLayout.SetItemsSource(StackLayoutNotification, bindObject);  
    

    Xaml

    <StackLayout x:Name="StackLayoutNotification">  
                <BindableLayout.ItemTemplate>  
                    <DataTemplate>  
                        <Grid>  
                    <Grid.RowDefinitions>  
                        <RowDefinition Height="Auto" />  
                        <RowDefinition Height="Auto" />  
                    </Grid.RowDefinitions>  
      
                    <Label TextColor="Yellow" Grid.Row="0" Text="{Binding id}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HorizontalTextAlignment="Start" FontSize="20" LineBreakMode="WordWrap" />  
                </Grid>  
                    </DataTemplate>  
                </BindableLayout.ItemTemplate>  
      </StackLayout>  
    

    For more information, you could refer to https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/bindable-layouts

    In addition, I noticed that there is only one object, you also could use MVVM and Data Binding to bind Root class.

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.